[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
Try adding root path.
app.get('/', function(req, res) {
res.sendFile('index.html', { root: __dirname });
});
I solve this by using path variable. The sample code will look like below.
var path = require("path");
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
})
It will redirects to index.html on localhost:8080 call.
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 are working on Root Directory then you can use this approach
res.sendFile(__dirname + '/FOLDER_IN_ROOT_DIRECTORY/index.html');
but if you are using Routes which is inside a folder lets say /Routes/someRoute.js
then you will need to do something like this
const path = require("path");
...
route.get("/some_route", (req, res) => {
res.sendFile(path.resolve('FOLDER_IN_ROOT_DIRECTORY/index.html')
});
I did this and now my app is working properly,
res.sendFile('your drive://your_subfolders//file.html');