Use Nodejs to build a static file server on Windows, even if you have no foundation can learn to Nodejs static file server, this article introduces the very detailed, very suitable for 0 basic introductory friends to learn.
First install Nodejs:
• Create a new node folder
• Download Node.exe to this folder
• Download NPM and extract it into the folder
• Now the node folder is like this
• Add the directory to the PATH environment variable
• Execute at command line
If the version number is obtained, it means that the Nodejs installation is complete
• Executing on the command line
NPM Config Set Registry https://registry.npm.taobao.org
Later install Nodejs module will be downloaded from the NPM image of Taobao
• If you want to release your own module to NPM, first switch the NPM registry back in.
NPM Config Set Registry https://registry.npmjs.org
Next, build a static file server
• Create a folder server, a folder Root,server is the server's JS code, root is the root directory
server folder inside the creation of JS files Index.js mime.js server.js
Index.js
var server = require ('./server.js ');
var rootpath = ' root ';
var SV = server.create ({
port: ' 9587 ',
Host: ' 127.0.0.1 ',
root:rootpath
});
mime.js
var types = {
"css": "Text/css",
"less": "Text/css",
"gif": "Image/gif",
"html": " Text/html ",
" Ejs ":" text/html ",
" ico ":" Image/x-icon ",
" JPEG ":" Image/jpeg ",
" jpg ":" image/ JPEG ",
" JS ":" Text/javascript ",
" JSON ":" Application/json ",
" PDF ":" Application/pdf ","
png ":" Image/png ",
" svg ":" Image/svg+xml ",
" swf ":" Application/x-shockwave-flash ",
" TIFF ":" Image/tiff ",
"TXT": "Text/plain",
"wav": "Audio/x-wav",
"wma": "Audio/x-ms-wma",
"wmv": "Video/x-ms-wmv",
"xml": "Text/xml",
"Default": "Text/plain"
};
Module.exports = function (EXT) {return
Types[ext] | | ' Text/plain '
}
Server.js
var http = require (' http ');
var path = require (' path ');
var fs = require (' FS ');
var url = require ("url");
var mime = require ('./mime.js '); function Getpromise (CBK) {return (new Promise (CBK));} exports.create = function (opts) {var root = Opts.root; var sv =
Http.createserver (); function request (request, response) {var pathname = decodeuricomponent (Url.parse (request.url). pathname); var Realpath = Path.resolve (Path.join (root, pathname))//The actual path of the request Getpromise (function (resolve, reject) {fs.exists (Realpath,
function (isexists) {//Determine whether the path exists isexists resolve (): Reject ();}); ). catch (function () {Reswrite (response, ' 404 ', ' html ', '
• In the server folder, hold down the shift button, right-click the blank area of the folder, click here to open the Command window, execute the command
The above is a small set for you to introduce in Windows with Nodejs static file server to build a simple method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!