2

Located on the api reference webpage, it is shown that Express.JS using the function

app.use('/', express.static(__dirname+'/public'));

should allow the static directory /public to be served.

here is my code.

    var app = require('express');
var bodyparser = require('body-parser');

app.use('/', express.static(__dirname+'/public'));

app.listen('3000');

the response I get from the terminal is "express is not defined".

I copied this directly from working code,

why isn't this working?

1
  • 1
    short answer: var app = require('express')(); Commented Oct 28, 2015 at 23:41

2 Answers 2

3

From the doc page you linked:

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

Compare that to what you have:

var app = require('express');

You need to define both express and app. Requiring express doesn't magically give you the variable. With any module, the require function loads the module into the variable you set it to.

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

Comments

2

change your require to :

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

the first will require the module, which later can be used for both the app itself and to call your static stuff. the second line will construct your app

1 Comment

after changing the required items here,

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.