A simple way to build a static file server with Nodejs on Windows _node.js

Source: Internet
Author: User
Tags json require

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

Node-v
Npm-v

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

Node Index.js

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.