LogoDuyệtSr. Data Engineer
HomeAboutPhotosInsightsCV

Footer

Logo

Resources

  • Rust Tiếng Việt
  • /archives
  • /series
  • /tags
  • Status

me@duyet.net

  • About
  • LinkedIn
  • Resume
  • Projects

© 2026 duyet.net | Sr. Data Engineer | 2026-02-27

Using ExpressJS to Display Static HTML File Content

Note: This post is over 12 years old. The information may be outdated.

In ExpressJs, there is a very simple way to send an HTML file to the browser using the method: res.sendFile(), which reads the content of the .html file and sends it to the browser, allowing us to quickly display the content of a page or some static page.

How to Use res.sendFile()

Using the sendFile() method is very simple, you just need to pass the only parameter which is the path to the HTML file you want to display.

Example

Create a directory

$ mkdir express-sendfile
$ cd express-sendfile

Initialize a Nodejs and ExpressJs application

$ npm init
$ npm install express --save

Create two files server.js and index.html

$ touch server.js index.html

Now, in our directory, we will have two files. Open server.js with some text editor, with the following content:

const express = require('express')
const app = express()
const path = require('path')

// viewed at http://localhost:8080
app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname + '/index.html'))
})

app.listen(8080)

Next is the index.html file, this is the content we need to display in the browser:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Sample Site</title>

    <link
      rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
    />
    <style>
      body {
        padding-top: 50px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="jumbotron">
        <h1>res.sendFile() Works!</h1>
      </div>
    </div>
  </body>
</html>

Ok, now let's check it out by running the command:

$ node server.js

Open the browser and go to: http://localhost:8080

Conclusion

res.sendFile() is a very easy-to-use and useful method in ExpressJs, you can make SinglePage applications, load content using AngularJs, static pages, etc. Additionally, ExpressJs provides many other tools to read and download files on the server.

Jan 23, 2015·11 years ago
|Javascript|
Node.js
|Edit|

Related Posts

Resting và Spreading JavaScript Objects

Resting và spreading càng ngày được ưa chuộng vì sự tiện lợi của nó, sau đây là 7 tricks với JavaScript objects.

Mar 27, 2019·7 years ago
Read more

Javascript Weekly #10

Javascript Weekly #10 gửi đến cộng đồng một số tin tức JS mới: top frameworks 2017, Angular Next 4.0, Voca xử lý string cực kỳ mạnh, Hyper 1.0 - terminal on top JS, Vuetify, ...

Dec 17, 2016·9 years ago
Read more

Javascript Weekly #9

Javascript Weekly #9: Date-DNS, chi tiết về các bản cập nhật mới của V8, React và Angular. ![](https://2.bp.blogspot.com/-3n1yGifwJdY/WEtxXVdp38I/AAAAAAAAgt4/ijum1QPHo6ENxNmPawMuGeKyxKfw2tfEACLcB/s1600/Screenshot%2Bfrom%2B2016-12-10%2B10-06-59.png) ## [date-fns: A Modern...

Dec 10, 2016·9 years ago
Read more

Yarn: A new package manager for JavaScript

Facebook has launched Yarn

Oct 13, 2016·9 years ago
Read more
On this page
  • How to Use res.sendFile()
  • Example
  • Conclusion
On this page
  • How to Use res.sendFile()
  • Example
  • Conclusion