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.
Related Posts:
- TypeError: Converting circular structure to JSON in nodejs
- TypeError: res.json is not a function
- Write / add data in JSON file using Node.js
- map function for objects (instead of arrays)
- “SyntaxError: Unexpected token < in JSON at position 0"
- How can a Discord bot create a hyperlink in a Discord message in an embed or in general? [closed]
- SyntaxError: Unexpected token o in JSON at position 1
- Babel 6 regeneratorRuntime is not defined
- SyntaxError: Unexpected token o in JSON at position 1
- What is JSONP, and why was it created?
- Babel 7 – ReferenceError: regeneratorRuntime is not defined
- How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
- Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai
- Client on Node.js: Uncaught ReferenceError: require is not defined
- How can a Discord bot create a hyperlink in a Discord message in an embed or in general?
- Array of JSON Objects
- Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai
- Error: Can’t set headers after they are sent to the client
- Fetch: POST JSON data
- Express error – TypeError: Router.use() requires middleware function but got a Object
- Heroku: “No default language could be detected for this app” error thrown for node app
- SyntaxError: Unexpected token o in JSON at position 1
- Heroku: “No default language could be detected for this app” error thrown for node app
- TypeError: Router.use() requires middleware function but got a Object
- How to read an external local JSON file in JavaScript?
- What is the convention in JSON for empty vs. null?
- ReferenceError: fetch is not defined
- ‘react-scripts’ is not recognized as an internal or external command
- After $npm install, Getting Error: Cannot find module ‘../lib/utils/unsupported.js’
- Nested JSON objects – do I have to use arrays for everything?
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- Invalid shorthand property initializer
- nodejs – error self signed certificate in certificate chain
- What’s the source of Error: getaddrinfo EAI_AGAIN?
- stop all instances of node.js server
- Using Node.js require vs. ES6 import/export
- Why is process.env.NODE_ENV undefined?
- jQuery AJAX cross domain
- How can I mock an ES6 module import using Jest?
- Uncaught TypeError: Cannot read property ‘props’ of null
- Express Render not working return error: No default engine was specified and no extension was provided
- How to use npm jquery module?
- TypeError: Router.use() requires middleware function but got a Object
- Node.js Error: connect ECONNREFUSED
- Loading local JSON file
- How to convert JSON object to JavaScript array?
- SyntaxError: Cannot use import statement outside a module
- How to handle ETIMEDOUT error?
- How to determine the installed webpack version
- Uncaught TypeError: data.push is not a function
- How to properly export an ES6 class in Node 4?
- Expressjs / Node.js – res.redirect() not loading page
- Using async/await with a forEach loop
- Failed to compile. Module not found: Can’t resolve ‘react-router-dom’
- MissingSchemaError: Schema hasn’t been registered for model “User”
- Convert Array to Object
- Basic Ajax send/receive with node.js
- Cannot find module ‘internal/fs’ after upgrading to node 7
- Make Axios send cookies in its requests automatically
- How to get JSON from URL in JavaScript?
- How do you put an image file in a json object?
- node-request – Getting error “SSL23_GET_SERVER_HELLO:unknown protocol”
- push object into array
- How to fix Cannot find module ‘typescript’ in Angular 4?
- Error: Cannot find module html
- JSON.parse() not working
- react router v^4.0.0 Uncaught TypeError: Cannot read property ‘location’ of undefined
- npm WARN enoent ENOENT: no such file or directory, open ‘C:\Users\Nuwanst\package.json’
- TypeError: res.status is not a function
- how to use react require syntax?
- SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
- Add new attribute (element) to JSON object using JavaScript
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- How to display raw JSON data on a HTML page
- Maximum call stack size exceeded on npm install
- SyntaxError: Use of const in strict mode
- JSON.parse unexpected token s
- How do you completely remove Ionic and Cordova installation from mac?
- How to filter JSON Data in JavaScript or jQuery?
- How to fix Error: listen EADDRINUSE while using NodeJS?
- Error: listen EACCES 0.0.0.0:80 OSx Node.js
- How to add a button dynamically using jquery
- How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
- What counts as CPU Intensive tasks (eg. sorting, searching etc?)
- events.js: 141 throw er; // Unhandled ‘error’ event
- Complex JSON nesting of objects and arrays
- socket.io – ReferenceError: io is not defined
- Uncaught SyntaxError: Unexpected token with JSON.parse
- Finding the max value of an attribute in an array of objects
- Defining a HTML template to append using JQuery
- What does `node –harmony` do?
- Error: Couldn’t find preset “es2015” relative to directory “/Users/username”
- Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (
) - Using Angular 2 without TypeScript Transpiler
- Is there a way to create interfaces in ES6 / Node 4?
- Implementing Isomorphic JavaScript (React JS) in WordPress?
- var is undefined in a Gutenberg block
- 404 Error Contact Form 7 on WordPress
- Pass data from wordpress to javascript in JSON
- Remove escape characters from JSON string pulled from a database