Error executing express using node

I am trying to run a very simple server on my Mac, so I can access the file from localhost.

I have node and express installed, and that’s all there is in my server file.

var express = require('express'), app = express(); app.use(express.static(__dirname, '/')); app.listen(8080); console.log("App listening on port 8080"); 

When I try to do:

 node server 

I get this as an answer:

 /Users/mt_slasher/node_modules/express/node_modules/serve-static/index.js:47 var opts = Object.create(options || null) ^ TypeError: Object prototype may only be an Object or null: / at Function.create (native) at Function.serveStatic (/Users/mt_slasher/node_modules/express/node_modules/serve-static/index.js:47:21) at Object.<anonymous> (/Users/mt_slasher/Desktop/My Projects/Basket/Site/server.js:4:23) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3 

I ran the same file on a Windows machine with the same files and had no problems.

After some digging, I found that this line is apparently the main culprit:

 app.use(express.static(__dirname, '/')); 

Can someone tell me what could happen?

+6
source share
3 answers

This is because you pass "/" as the second parameter (parameters)

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

See service-static:

 function serveStatic(root, options) ... 

https://github.com/expressjs/serve-static/blob/master/index.js

Also note that it would be better to use a different directory than your root, for example. express.static(__dirname + '/public') so as not to expose your root.

+8
source

express.static used to determine the directory where your "static" files are located, here for more information .

It only accepts a string whose path you want to be static:

So your code should be:

 app.use(express.static('/')); 

or

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

But that doesn't make much sense, imho.

Delete the line or determine the real path where your resource files are.

+2
source

The second parameter you pass to express.static is invalid. remove the second parameter. app.use (express.static (__ directory_name));

0
source

Source: https://habr.com/ru/post/989961/


All Articles