Your location:Tech News>News>Script>Several ways to set up a local file server with nodeJS Summary

Latest News

Several ways to set up a local file server with nodeJS Summary



  Nodejs server set up steps:

  1.Nodejs installation services, node corresponds to the apache server

  2.In their definition of the new directory servers such as file server.js

  For example, I was in E: Creating the server \ PhpProject \ HTML5 under \ websocket.js file

  var http = require ( ''http''); // http module incorporated

  // open the service, listening 8888 port

  // port number of the best 6000 or more

  var server = http.createServer (function (req, res) {

  console.log ( ''client connection''); // create a successful connection is displayed in the background

  // http request is a reference state, the connection is successful 200

  // write head after a successful connection information to the client

  res.writeHeader (200, {

  ''Content-type'': ''text / html; charset = "utf-8"''


  res.Write ( ''This is the body''); // display to the client

  res.end ();

  }).listen (8888);

  console.log ( ''open server success'');

  3.In cmd switched into the console server cd.Js directory is located, and then perform node server.js command

  When the console displays "Server open success" then the node server has been established

  4.Access server in your browser

  Enter in your browser

  localhost: 8888, the browser displays "This is the body".

  View cmd console displays "client connection."

  The above operation may be performed in a plurality of browser windows, each browser window will correspond to a "client connection"

  The above steps are completed, node service set up is completed.Here''s how to build a node by text service to access local sites / html text file

  Access to the local site files

  1.Create a file server2 service node in a custom directory.js

  var http = require ( ''http'');

  var fs = require ( ''fs''); // file reading module incorporated

  var documentRoot = ''E: / PhpProject / html5 / websocket / www'';

  // store directory files need to be accessed

  var server = http.createServer (function (req, res) {

  var url = req.url;

  url // client input, for example, if you enter localhost: 8888 / index.html

  // this is url == / index.html

  var file = documentRoot + url;

  console.log (url);

  // E: / PhpProject / html5 / websocket / www / index.html

  fs.readFile (file, function (err, data) {

  if (err) {

  res.writeHeader (404, {

  ''Content-type'': ''text / html; charset = "utf-8"''


  res.write ( ''

404 Error

Page you are looking for does not exist


  res.end ();

  } Else {

  res.writeHeader (200, {

  ''Content-type'': ''text / html; charset = "utf-8"''


  res.write (data); // the index.html displayed in the client

  res.end ();



  }).listen (8888);

  console.log ( ''open server success'');

  2.Creating index.html file

  To access index.html file, of course, you have to have this document, or server read failure, return 404

  3.In cmd console cd to server2.Execution node server2 under js directory.js command

  Turn on the server

  4.In the browser, enter localhost: 8888 / index.html access the file

  If the computer does not install nodejs students can download and install the official first-node node program.

  After installation nodejs verify whether the installation is successful!If there is indicates that the node is already installed Holds go up!

  After installing open cmd input node npm install anywhere -g installed anywhere, and then wait until the following interface.

  After the above two steps are ready, everything is ready, only a strong wind it!Cmd page found in the path you want to set up the server, then type the path to the current: anywhere 8860

  Then the browser will automatically open local access URL, a simple node server and it was okay we built!



  anywhere recommend installing the global

  node is recommended to use 32-bit, 64-bit compatibility is not currently a good 32

  If we open the html file directly he is file: /// opened, but this way sometimes encounter cross-domain issue: "cross-origin", so we need to build a simple local server, Nodejs can we meet this demand:

  For example, my local server js file is such (I put this server.js placed Nodejs installation directory):

  var connect = require ( "connect");

  var serveStatic = require ( "serve-static");

  var app = connect ();

  app.use (serveStatic ( "C: \\ xxx \\ xxx \\ xxx \\ project folder"));

  app.listen (5000);

  Running, then just run: node server.js on it

  After running we can enter in the browser localhost: 5000 to access the project folder in the file.(If the index is.html file can be omitted, the default is to load this file)

  That''s all for this article, I want to be helpful to learn, I hope you will support script Home.

  You may also be interested in the article: Method nodejs set up a local server and access files nodejs set up a local server instance easily solve cross-domain Question 3 minute quick set up nodejs local server running the test method html / jsNodeJs set up mobile access to local servers to explain the use of nodeJs the next step method anywhere up a local server environment for Windows NodeJS quickly set up a local server nodejs how to build a local web server test access static resources to solve the problem nodejs setting up a local http server tutorial nodejs set up a local server access and file operations example

Recommend article

Relate article