浏览代码

Create a simple server.

NaotoshiFujita 3 年之前
父节点
当前提交
8a0e4376ae
共有 1 个文件被更改,包括 32 次插入0 次删除
  1. 32 0
      scripts/serve.js

+ 32 - 0
scripts/serve.js

@@ -0,0 +1,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 );