-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
57 lines (47 loc) · 1.11 KB
/
index.mjs
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
55
56
57
/**
* Basic HTTP Server to test Ice.js SSR
* @author skitsanos
* @version 1.0.0
*/
import {createReadStream} from 'fs';
import http from 'http';
import {extname, join} from 'path';
import {renderToHTML} from './dist/server/index.mjs';
const root = join('.', 'dist');
const getContentType = {
'.js': 'text/javascript',
'.css': 'text/css',
'.jpg': 'image/jpeg',
'.png': 'image/jpeg'
};
http.createServer(async (req, res) =>
{
const ext = extname(req.url);
const header = {
'Content-Type': getContentType[ext] || 'text/html'
};
res.writeHead(200, header);
if (ext)
{
// static file url
const path = join(root, req.url);
const stream = createReadStream(path);
stream.on('error', (error) =>
{
console.log(error);
res.writeHead(404, 'Not Found');
res.end();
});
stream.pipe(res);
}
else
{
console.log(req.url);
// url render
const {value} = await renderToHTML({
req
});
res.write(value);
res.end();
}
}).listen(3000);