最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS Express is not defined when I try to serve static files - Stack Overflow

programmeradmin2浏览0评论

I am trying to serve static content for the app from the "www" directory in the application directory.

my folders look like this:

    -www
    --node_modules
    --js
    --index.js
    --index.html
    --package.json

I want to use the main.js file that is inside the js folder but when I try to use the

    <script src="/js/main.js"></script> 

in the index.html, my console give me a GET error and 404 file not found.

My code looks something like this:

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);

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

    app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
    });

when I try to run the server I get the error:

    app.use(express.static(__dirname + '/www');
    ReferenceError: express is not defined

Can someone give me a hint of why this is happening?

I am trying to serve static content for the app from the "www" directory in the application directory.

my folders look like this:

    -www
    --node_modules
    --js
    --index.js
    --index.html
    --package.json

I want to use the main.js file that is inside the js folder but when I try to use the

    <script src="/js/main.js"></script> 

in the index.html, my console give me a GET error and 404 file not found.

My code looks something like this:

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);

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

    app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
    });

when I try to run the server I get the error:

    app.use(express.static(__dirname + '/www');
    ReferenceError: express is not defined

Can someone give me a hint of why this is happening?

Share Improve this question asked Apr 25, 2016 at 15:55 bernasbernas 731 gold badge1 silver badge6 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 11

You forgot to import express.

You also should move your non static files outside "www" since that's the path you want for the "static files".

 var express = require('express');
 var path = require('path');
 var app = express();
 var http = require('http').Server(app);
 var io = require('socket.io')(http);

 var publicPath = path.resolve(__dirname, 'www');


 app.use(express.static(publicPath));
 app.get('/', function(req, res){
     res.sendFile('index.html', {root: publicPath});
 });

In your code, you haven't declared any variable named as express. Without variable named as express how you can call it's variables/functions/keys.

To use express./anything/ you have to declare express variable in your code. that can be done via

var express = require('express');

With Webstorm, it didn't work if I used :

const express = require('express');

But work with : var express = require('express');

Maybe it's related to Webstorm, maybe it's not a "good" answer, but for me it solves this issue.

If your answer says express is not defined, then in your cmd or bash terminal, use the following command:-

$ npm install express

After the install successfully completes, require the express module using the following:-

const express=require('express');

run your code. Remember to try it first in a localhost server. Add a port listener to ur code and try it out.

发布评论

评论列表(0)

  1. 暂无评论