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 badges4 Answers
Reset to default 11You 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.