node 開啓本地服務器javascript
新建server.js文件css
終端命令node serverhtml
server.js代碼java
var http = require('http'); var url = require('url'); var fs = require('fs'); //fs var server = http.createServer(function (req, res) { var pathname = url.parse(req.url).pathname; // var realPath = "process_admin_20171009" + pathname;//全部文件都存在與resources目錄下 var realPath = pathname;//全部文件都存在與resources目錄下 console.log(realPath); fs.exists(realPath, function (exists) {//判斷文件是否存在 if (!exists) { res.writeHead(404,{"Content-Type":"text/plain"}); res.write("404\nNot Found!\n"); res.end(); } else { fs.readFile(realPath, "binary", function (err, file) { if(realPath.indexOf('.css') != -1){ res.writeHead(200, { 'Content-Type': 'text/css' }); } if(realPath.indexOf('.js') != -1){ res.writeHead(200, { 'Content-Type': 'application/javascript' }); } if(realPath.indexOf('.html') != -1){ res.writeHead(200, { 'Content-Type': 'text/html' }); } res.write(file, "binary"); res.end(); }); } }); }); server.listen(8089); console.log("靜態資源服務器已啓動");