Node JS Express is not defined when I try to serve static files

node serve static files
express serve html
express.static not working
express js
express node static
express return static file
express serve static react
express static content-type

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

my folders look like this:


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('')(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?

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('')(http);

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

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

Node JS Express is not defined when I try to serve static files, 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  express.static is a piece of middleware that comes built into Express, it’s purpose is to try to find and return the static file requested. The parameter we pass to the express.static function is the name of the directory we want Express to serve files from, in our case it’s public .

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');

Serving static files in Express, To serve static files such as images, CSS files, and JavaScript files, use the the files relative to the static directory, so the name of the static directory is not part of the Express looks up the files in the order in which you set the static directories function is relative to the directory from where you launch your node process. We are using Express.js 4.17.1 version to serve static files such as images, CSS, JS using the built-in express.static middleware function. express.static ( root, [ options ] ) The root parameter associated with a root folder from where static assets are being served.

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.

Serving Static Files in Express.js, Express makes serving static files ridiculously easy! This means that we should not put anything secret/sensitive in there, so keep any files with but in short, a middleware function is a function that Express passes built into Express, it's purpose is to try to find and return the static file node index.js. It will only consider requests that don't have a period in their filename. So if you serve your CSS files with a .css extension, there shouldn't be a problem (but it checks for the existence of a .html file first, if it doesn't exist it will just do nothing and let the static middleware handle it as-is). – robertklep Jun 3 '13 at 12:31

ExpressJS - Serving static files, Express, by default does not allow you to serve static files. Note that the root route is now set to your public dir, so all static files you load will be provide a path prefix like '/static', you need to include the following code in your index.js file − Here is my one-file/lightweight node.js static file web-server pet project with no-dependency that I believe is a quick and rich tool which its use is as easy as issuing this command on your Linux/Unix/macOS terminal (or termux on Android) when node.js (or nodejs-legacy on Debian/Ubuntu) is installed:

Serving Static Files with Node and Express.js, We are all set, let's start coding! Serving Files with Express. There are two ways to serve static files using Express: Serving a single file by configuring a path to the  Reason Node.Js does not server static content on it's own, routes has to defined for serving static content via Node. Solution (Manual): var express = require('express'), path = require('path'), app = express(); app.get('/index.html',function(req,res) { res.sendFile(path.join(__dirname + '/index.html')); }); app.get('/css/app.css',function(req,res) { res.sendFile(path.join(__dirname + '/css/app.css')); }); app.get('/js/app.js',function(req,res) { res.sendFile(path.join(__dirname + '/js/app.

expressjs/serve-static: Serve static files, Contribute to expressjs/serve-static development by creating an account on GitHub. This is a Node.js module available through the npm registry. When set, if a file is not found, the given extensions will be added to the file name and search for. var express = require('express') var path = require('path') var serveStatic  Create a new middleware function to serve files from within a given root directory. The file to serve will be determined by combining req.url with the provided root directory. When a file is not found, instead of sending a 404 response, this module will instead call next() to move on to the next middleware, allowing for stacking and fall-backs