How to create a simple html server using express js

I'm new to node.js I want to create a simple static.js static file server, but I have some problems. I have installed express.js 4.2 everywhere:

npm install -g express-generator 

I have this code in httpsrv.js:

 var http = require('http'); var express = require('express'); var app = express(); app.use('/', express.static(__dirname + '/public')); app.listen(3000, function() { console.log('listening')}); 

I'm not sure that everything is in order. I think this is not enough, but I can not start it with an error: I can not find the express module.

I want to create a simple HTTP server that can work from a specific folder ("\ public", for example), and I use the language .html. I found a lot of nonsense on the Internet, I do not want to use this .Jade thing, and I do not want to create empty web applications using express, etc. I want an express.js http server that can work like Apache, and can put html pages from the specified folder first. Can someone help me with this, suggest a good article that explains step by step because I am starting.

+7
javascript express fileserver
source share
3 answers

If you are just trying to serve static files from a directory called "public", you might be lucky with this application:

 var path = require('path'); var express = require('express'); var app = express(); var staticPath = path.join(__dirname, '/public'); app.use(express.static(staticPath)); app.listen(3000, function() { console.log('listening'); }); 

You need to make sure Express is installed. You will probably run npm install express --save in the same directory as the above JavaScript file. When everything is ready, you run node the_name_of_the_file_above.js to start your server.

+18
source share

first install the express module, in addition to the express generator

 npm install express 

try deleting the public

 var express = require('express'); var app = express(); app.use('/', express.static(__dirname)); app.listen(3000, function() { console.log('listening')}); 

works fine.

+6
source share

This problem does not even require any code or framework; install the http server from npm , navigate to the folder on the command line and run this command:

 http-server 

And it will deploy a lightweight HTTP server and will show static content from a folder that can be viewed using http: // localhost

0
source share

All Articles