Question

Node js server is crashing

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.

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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.