Although the http core module in Node.js can be used to implement a web server, there is a lot of work to do, such as detecting the content type of each file
So we decided to use the Connect module, the very popular Express web framework is built on top of Connect
A fast static web server:
var connect = require('connect');
connect.createServer(
connect.static(__dirname)
).listen(8080);
To run this code you need to have Connect installed, of course you can do npm install connect directly, but preferably in your project directory
Create a new package.json and declare this dependency in it, so that others can understand how your program works.
In fact, instead of manually creating a package.json file, you can execute npm init, which will create a manifest file for the current directory
Just follow the prompts, and then execute npm install --save connect, which will install connect, and at the same time in package.json
Dependency information on connect is saved in .
The createServer method takes advantage of Node.js' http.createServer, but adds a few things of its own. The static component in it
Used to serve files from the current directory (__dirname).
After everything is ready, if the file is called server.js, then execute node server.js to run the server we just developed
You can access static files by visiting http://localhost:8080/filename in your browser
There is a package called glance that can also achieve the effect just now
After installing glance, in a directory, execute glance -p 9999
Access a file in the current directory of http://localhost:9999/ to access it
glance --help can view help information