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
- How to make a discord bot create an invite for every server it joins?
- map function for objects (instead of arrays)
- Start script missing error when running npm start
- “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]
- How to you store data for each discord user in the discord server?
- 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
- Use a JSON array with objects with javascript
- Client on Node.js: Uncaught ReferenceError: require is not defined
- Error: Can’t set headers after they are sent to the client
- 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
- Chrome sendrequest error: TypeError: Converting circular structure to JSON
- require is not defined? Node.js
- Client on Node.js: Uncaught ReferenceError: require is not defined
- Error: Can’t set headers after they are sent to the client
- TypeError: Router.use() requires middleware function but got a Object
- Fetch: POST JSON data
- Express error – TypeError: Router.use() requires middleware function but got a Object
- Cannot GET / Nodejs Error
- Uncaught SyntaxError: Unexpected token u in JSON at position 0
- What is NODE_ENV and how to use it in Express?
- Node.js heap out of memory
- Reactjs – Form input validation
- await is only valid in async function
- How to set NODE_ENV to production/development in OS X
- Unexpected token u in JSON at position 0 (but only sometimes)
- await is only valid in async function
- How to read an external local JSON file in JavaScript?
- 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
- SyntaxError: Unexpected token o in JSON at position 1
- TypeError: Router.use() requires middleware function but got a Object
- How to read an external local JSON file in JavaScript?
- What does “res.render” do, and what does the html file look like?
- How to obtain username from Promise
? - NPM ELIFECYCLE error – using node server.js command
- What is the convention in JSON for empty vs. null?
- Node.js – SyntaxError: Unexpected token import
- What is the “right” JSON date format?
- 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?
- Uncaught TypeError: Cannot read property ‘length’ of undefined
- Node.js – SyntaxError: Unexpected token import
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- What is the “right” JSON date format?
- 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
- How to compare arrays in JavaScript?
- Failed to load resource: net::ERR_FILE_NOT_FOUND loading json.js
- ReferenceError : window is not defined at object.
Node.js - Why is process.env.NODE_ENV undefined?
- Node.js Port 3000 already in use but it actually isn’t?
- Unable to verify leaf signature
- jQuery AJAX cross domain
- npm – EPERM: operation not permitted on Windows
- How can I mock an ES6 module import using Jest?
- SyntaxError: Unexpected token o in JSON at position 1
- Uncaught TypeError: Cannot read property ‘props’ of null
- AJAX response error: net::ERR_EMPTY_RESPONSE
- 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
- TypeError: app.use() requires middleware functions
- Loading local JSON file
- No overload matches this call. Type ‘string’ is not assignable to type ‘Signals’
- How to convert JSON object to JavaScript array?
- SyntaxError: Cannot use import statement outside a module
- Node.js document is not defined
- How to handle ETIMEDOUT error?
- How to determine the installed webpack version
- Uncaught TypeError: data.push is not a function
- React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
- XMLHttpRequest module not defined/found
- Node update a specific package
- How to convert selected HTML to Json?
- How to properly export an ES6 class in Node 4?
- Express error – TypeError: Router.use() requires middleware function but got a Object
- Expressjs / Node.js – res.redirect() not loading page
- Using async/await with a forEach loop
- Why is my asynchronous function returning Promise {
} instead of a value? - Failed to compile. Module not found: Can’t resolve ‘react-router-dom’
- NodeJs : TypeError: require(…) is not a function