Send Static Files in Express with sendFile()

Send Static Files in Express with sendFile()

Send Static Files in Express with sendFile()

In this post, we will give you information about Send Static Files in Express with sendFile(). Here we will give you detail about Send Static Files in Express with sendFile() And how to use it also give you a demo for it if it is necessary.

Express’ sendFile() function lets you send a raw file as a response to an HTTP request. You can think of res.sendFile() as Express’ static middleware for a single endpoint.

Using sendFile()

Suppose you have an HTML file test.html that looks like this:

<h1>Hello, World</h1>

You can make Express serve this HTML file as an HTTP response using res.sendFile() by passing the path to test.html. Note that the path must be absolute unless you specify the root option.

app.get('/myendpoint', (req, res) => {

If you don’t want to specify the absolute path, you can pass the root option to
specify the directory the path is relative to.

app.get('/myendpoint', (req, res) => {
  res.sendFile('test.html', { root: __dirname });

Want to become your team’s Express expert? There’s no better way to really grok a framework than to write your own
clone from scratch. In 15 concise pages, this tutorial walks you through how to write a simplified clone of Express
called Espresso.
Get your copy!

Espresso supports:

  • Route handlers, like ‘app.get()’ and ‘’
  • Express-compatible middleware, like ‘app.use(require(‘cors’)())’
  • Express 4.0 style subrouters
See also  Express Response JSON - onlinecode

As a bonus, Espresso also supports async functions, unlike Express.

Get the tutorial and master Express today!


I need more information on what you are looking for. Here are a few options for what you might mean by ‘express’:

  • Express train: A train that makes fewer stops than a regular train, and therefore travels faster.
  • Express delivery: A service that delivers packages quickly, usually within 24 hours.
  • Express yourself: To communicate your thoughts and feelings in a clear and direct way.
  • Express lane: A lane on a highway that is reserved for cars that are traveling faster than the other lanes.
  • Expresso: A strong, concentrated coffee.

Please let me know if any of these are what you were looking for. If not, please provide more information so I can better assist you.

See also  Serving Static Files in Express

Here are some examples of how to use the word ‘express’ in a sentence:

  • I took the express train to work today.
  • I ordered the package to be delivered express.
  • She expressed her anger by slamming the door.
  • The express lane was moving much faster than the other lanes.
  • I love the strong taste of espresso.

Hope this code and post will help you for implementing Send Static Files in Express with sendFile() – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

See also  Getting the Request Body in Express

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US