-
Notifications
You must be signed in to change notification settings - Fork 8
/
devserver.js
54 lines (46 loc) · 1.45 KB
/
devserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// this is a simple dev server for testing Smalltalk78
// it serves static files and directory listings
// and allows uploading files (e.g. to the /updates/ directory)
// usage:
// npm i
// node devserver.js
// in Smalltalk78:
// "upload a file"
// user fileString: 'http:foo/bar.txt' ← 'hello'.
// "use local update stream"
// updateURL ← 'http:updates/'.
// "see all updates"
// user fileString: updateURL
const express = require('express');
const serveIndex = require('serve-index')
const fs = require('fs');
const path = require('path');
const app = express();
const port = 8000;
// log requests
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
// set req.body to raw body
app.use(express.raw({ type: '*/*' }));
// upload files
app.put("/*", (req, res, next) => {
const filepath = path.join(".", req.path);
const dir = filepath.slice(0, filepath.lastIndexOf('/'));
if (!fs.existsSync(dir)) {
console.log(`==> creating directory ${dir}`);
fs.mkdirSync(dir, { recursive: true });
}
console.log(`==> storing ${req.body.length} bytes at ${filepath}`);
const stream = fs.createWriteStream(filepath);
stream.write(req.body);
stream.end();
res.send(`Uploaded ${req.path}`);
});
// serve static files and directory listings
app.use(express.static('.'), serveIndex('.'));
// start server
app.listen(port, () => {
console.log(`Server listening at ${port}`);
});