Hello.
I am following this instruction How To Create a Web Server in Node.js with the HTTP Module
When i start node js server. it is working normally. But if i call from chrome browser. Server is crashing.
root@ubuntu-s-1vcpu-1gb-ams3-01:~/first-servers# node htmlFile.js
Server is running on http://161.35.144.72:8000
/root/first-servers/htmlFile.js:7
fs.readFile(__dirname + "/index.html")
^
TypeError: Cannot read property 'readFile' of undefined
at Server.requestListener (/root/first-servers/htmlFile.js:7:8)
at emitTwo (events.js:126:13)
at Server.emit (events.js:214:7)
at parserOnIncoming (_http_server.js:619:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:115:23)
root@ubuntu-s-1vcpu-1gb-ams3-01:~/first-servers#
root@ubuntu-s-1vcpu-1gb-ams3-01:~/first-servers# ls -a
. .. csv.js hello.js html.js htmlFile.js index.html json.js routes.js
root@ubuntu-s-1vcpu-1gb-ams3-01:~/first-servers#
Thank you.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey!
You’re probably missing the fs (NodeJS’s file system module) import, adding this to the first line of your file might work:
const fs = require('fs');
If it complains about the const, you’re probably using an older version of NodeJS, you could either upgrade to a new one or change the const to var.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.