-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
32 lines (26 loc) · 836 Bytes
/
serve.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
const http = require( 'http' );
const path = require( 'path' );
const fs = require( 'fs' ).promises;
const server = http.createServer();
const mime = {
'.html': 'text/html',
'.css' : 'text/css',
'.jpg' : 'image/jpeg',
'.js' : 'application/javascript',
};
server.on( 'request', async ( request, response ) => {
const { url } = request;
let fullPath;
if ( url === '/' ) {
fullPath = path.resolve( './src/js/test/html/index.html' );
} else if ( url.startsWith( '/' ) ) {
fullPath = path.resolve( `.${ url }` );
} else {
fullPath = url;
}
const type = mime[ path.extname( fullPath ) ] || 'text/plain';
const buffer = await fs.readFile( fullPath ).catch( e => console.warn( e ) );
response.writeHead( 200, { 'Content-Type': type } );
response.end( buffer );
} );
server.listen( 3000 );