0

I want to read an HTML file.

enter image description here

My HTML content:

<html>
  <hear>
    <title>Learn NodeJS</title>
  </head>

  <body>
    <center>
      <h1>Learn NodeJS with Khuong Pham</h1>
      <img width="400" src="/nodejs.png" />
    </center>
  </body>
</html>

I've tried:

const http = require('http')
const fs = require('fs')
const express = require('express')

const app = express()
const folderPath = __dirname + '/public_files'

app.use(express.static(folderPath))

http.createServer(function(request, response) {
  var filePath = folderPath + '/index.html'
  console.log(filePath)

  fs.access(filePath, fs.F_OK | fs.R_OK, function(err) {
    if (err) {
      response.writeHead(404, { 'Content-Type' : 'text/html' })
      response.end('<h1>File not found</h1>')
    } else {
      fs.readFile(filePath, function(err, contentFile){
        if (!err) {
          response.writeHead(200, { 'Content-Type' : 'text/html' })
          response.end(contentFile)
        } else {
          response.writeHead(500, { 'Content-Type' : 'text/html' })
          response.end('<h1>Can not read this content</h1>')
        }
      })
    }
  })
}).listen(3500)

But when I access http://localhost:3500/, it says:

enter image description here

8
  • If you change this line response.end(contentFile) by these: response.write(contentFile); response.end(); does it work? Commented Nov 1, 2016 at 15:23
  • It still has the same problem. Commented Nov 1, 2016 at 15:25
  • Please add this before the console.log(filePath) line: console.log(baseURI.pathName). What's the result? Commented Nov 1, 2016 at 15:35
  • Hi @RicardoPontual, it prints undefined Commented Nov 1, 2016 at 15:37
  • That's the problem. Try to set the filePath like this for a test: var filePath=folderPath + '/index.html' Commented Nov 1, 2016 at 15:49

1 Answer 1

1

You are mixing two methods here. Firstly you are trying to use express, but later you are starting your own server using http.createServer Instead you should use express to do so.

Your js should be something similar to below. Have not tested below code. Edit it approiately. This is just to show the idea.

const http = require('http')
const fs = require('fs')
const express = require('express')

const app = express()
const folderPath = __dirname + '/public_files'

//mount your static paths
// renders your image and index.html
app.use(express.static(folderPath))

// renders your index.html
app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '/index.html'));
});

//mount your other paths
// in this case render 404.
app.get("*",function (req, res) {
  res.status(404).send(''<h1>File not found</h1>'');
});

//start the server.
app.listen(3500, function () {
 console.log('Example app listening on port 3500!');
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.