help icon indicating copy to clipboard operation
help copied to clipboard

node-static file server error 404

Open zankhanarana9 opened this issue 7 years ago • 2 comments

I am trying to serve static files using node-static. Later I intend to run this as windows service using nssm. I have run this before but don't know why, it doesn't work now.

Below is my code:

var http = require('http');

var nStatic = require('node-static');

var fileServer = new nStatic.Server(); ---> I also tried new nStatic.Server('filepath') //setting middleware

http.createServer(function (req, res) { console.log("Server Started"); fileServer.serve(req, res); }).listen(9000); I start the server using node server.js on command prompt. This is what I see on the browser

image

There are no error messages on Developer Console

On command prompt, I see the message "Server Started" node -v 10.15.2 npm -v 6.4.1

Can anyone please tell me what is that I am doing wrong ?

zankhanarana9 avatar Feb 28 '19 21:02 zankhanarana9

Hi @zankhanarana9 !

This code works fine:

var http = require("http");
var nStatic = require("node-static");

var fileServer = new nStatic.Server();

http
  .createServer(function(req, res) {
    fileServer.serve(req, res);
  })
  .listen(9000);

I tried with the the following folder structure:

- node_modules
- index.js
- test.txt

When I start the server with node index.js, I can go to http://localhost:9000/test.txt and see the content of my txt file:

image

Can you show your folder structure? Most likely you forgot to add a file name you want to get from your folder in your address bar (see /test.txt in my example)

mikhalev-im avatar Mar 01 '19 16:03 mikhalev-im

Looks like my directory structure was incorrect and so, the error 404. I added few folders and files and it works fine now.

Thank you.

zankhanarana9 avatar Mar 07 '19 22:03 zankhanarana9