[add] So my next problem is that when i try adding a new dependence (npm install --save socket.io). The JSON file is also valid. I get this error: Failed to parse json
npm ERR! Unexpected string
npm ERR! File: /Users/John/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse
So I've been trying to figure out why this error has been returning. All of the files (HTML,JSON,JS) are inside the same folder on my desktop. I'm using node.js and socket.io
This is my JS file:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.sendFile('index.html');
});
http.listen(3000,function(){
console.log('listening on : 3000');
});
This is what is getting returned:
MacBook-Pro:~ John$ node /Users/John/Desktop/Chatapp/index.js
listening on : 3000
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
at /Users/John/Desktop/Chatapp/index.js:5:7
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at /Users/John/node_modules/express/lib/router/index.js:234:24
at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
at /Users/John/node_modules/express/lib/router/index.js:228:12
at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
at /Users/John/Desktop/Chatapp/index.js:5:7
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at /Users/John/node_modules/express/lib/router/index.js:234:24
at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
at /Users/John/node_modules/express/lib/router/index.js:228:12
at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)
The error is pretty clear, you need to specify an absolute (instead of relative) path and/or set root
in the config object for res.sendFile()
. Examples:
// assuming index.html is in the same directory as this script
res.sendFile(__dirname + '/index.html');
or specify a root (which is used as the base path for the first argument to res.sendFile()
:
res.sendFile('index.html', { root: __dirname });
Specifying the root
path is more useful when you're passing a user-generated file path which could potentially contain malformed/malicious parts like ..
(e.g. ../../../../../../etc/passwd
). Setting the root
path prevents such malicious paths from being used to access files outside of that base path.
in .mjs files we for now don't have __dirname
hence
res.sendFile('index.html', { root: '.' })
Try adding root path.
app.get('/', function(req, res) {
res.sendFile('index.html', { root: __dirname });
});
I used the code below and tried to show the sitemap.xml file
router.get('/sitemap.xml', function (req, res) {
res.sendFile('sitemap.xml', { root: '.' });
});
If you trust the path, path.resolve is an option:
var path = require('path');
// All other routes should redirect to the index.html
app.route('/*')
.get(function(req, res) {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With