9

I'm new in node.js I want to create a simple express.js static file server, but I have some issues. I have been installed express.js 4.2 globally like this:

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 is it ok I guess it is not enough, but I cant run it it's failed with error: cannot find module 'express'.

I want to create a simple http server which can serve from specific folder("\public" e.g.) and I'm using .html language. I found on the internet a many bullshit, I don't want to use this .jade thing and I don't want to create a empty web app with express etc. I want express.js http server which can operate like Apache and can serve a static html pages first from a specified folder. Can anybody help me on this, suggest a good article which is explain a step by step, because I'm beginner.

4
  • 2
    Have you followed the steps here: expressjs.com/guide.html Commented Jul 1, 2014 at 19:22
  • 1
    I don't think express-generator is what you need installed, you should run npm install -g express, and than try loading it. Commented Jul 1, 2014 at 19:23
  • I also have installed express 4.2, but I have been read express 4.x need this express-generator also. Commented Jul 1, 2014 at 19:36
  • 1
    ...where did that httpsrv.js come from? It doesn't follow express 4.x conventions. It looks like it is from an older 3.x example. The express generator creates app.js that exports much of what is needed and npm start launches the server. It doesn't look like you used the generator or you used it and...ignored the output? Commented Jul 1, 2014 at 20:13

3 Answers 3

23

If you're just trying to serve static files from a directory called "public", you might have luck with an app like this:

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'll need to make sure Express is installed. You'll probably run npm install express --save in the same directory as the above JavaScript file. Once you're all ready, you'll run node the_name_of_the_file_above.js to start your server.

Sign up to request clarification or add additional context in comments.

4 Comments

Now it almost good, but when I start the server and try to see in the browser, I get this message: "Cannot GET / ". I guess its only something path problem. My javascript server file is placed in the same "/public" directory what I want to serve is it a problem?
Change it to path.join(__dirname, '/public')
how to run the app on https and http both?
@JitendraPancholi It depends, but you can use the built-in https module to do this. Happy to find an example if you want more guidance.
12

first install express module inspite of express-generator

npm install express

try this removing public

var express = require('express');
var app = express();

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

it is working fine.

Comments

1

This problem shouldn't even need any code or frameworks; install http-server from npm, navigate to the folder in the command prompt and run this command:

http-server

And it will spin up a lightweight http server and serve static content up from the folder immediately which can be viewed using http://localhost

2 Comments

you are not even answering the question.
His/her requirement was to set up a simple server to serve static pages from a folder; this package does exactly that. You don't need to use a framework like Express for this purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.