Monday 18 March 2019

Express: res.sendFile(path [, options] [, fn]): Send file at given path


Transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension. The method invokes the callback function fn(err) when the transfer is complete or when an error occurs.

For more information on all the options, refer below link.

index.js
'use strict'

const express = require('express')

const app = express()

const fileName = 'C:\\Users\\Public\\ntuser.dat'

  var options = {
    headers: {
        'x-timestamp': Date.now(),
        'x-sent': true
    }
  };

app.get('/', (req, res, next) => {
 res.sendFile(fileName, options, function (err) {
  if (err) {
   next(err)
  } else {
   console.log('Sent:', fileName)
  }
 })
})

app.use((err, req, res, next) => {
    if (err) {
  console.error(err)
  res.status(500);
  return res.send('500: Internal server error');
    }
 
 return res.status(200).send('OK')
 console.log()

    
});

const port = 3000
app.listen(port, () => console.log(`Application started listening on port ${port}!`))



Previous                                                 Next                                                 Home

No comments:

Post a Comment