Christopher Allen
Christopher Allen

Reputation: 8599

Express JS Node JS Static Web Development

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?

Upvotes: 2

Views: 55

Answers (2)

Nir Levy
Nir Levy

Reputation: 12953

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

Upvotes: 2

jperezov
jperezov

Reputation: 3181

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.

Upvotes: 3

Related Questions