Categories
JavaScript Answers

How to redirect in Express.js while passing some context?

Sometimes, we want to redirect in Express.js while passing some context.

In this article, we’ll look at how to redirect in Express.js while passing some context.

How to redirect in Express.js while passing some context?

To redirect in Express.js while passing some context, we can call res.redirect with a query string appended to the path.

For instance, we write

app.get('/category', (req, res) => {
  const string = encodeURIComponent('baz baz');
  res.redirect('/?foo=' + string);
});

app.get('/', (req, res) => {
  const passedVariable = req.query.foo;
  // ...
});

to call encodeURIComponent in the /category route handler to encode the URL parameter value.

And then we call res.redirect with the URL path and query string appended after it.

Then in the / route handler, we get the value of the foo query parameter with req.query.foo.

Conclusion

To redirect in Express.js while passing some context, we can call res.redirect with a query string appended to the path.

Categories
JavaScript Answers

How to extract request HTTP headers from a request using Node.js Connect?

Sometimes, we want to extract request HTTP headers from a request using Node.js Connect.

In this article, we’ll look at how to extract request HTTP headers from a request using Node.js Connect.

How to extract request HTTP headers from a request using Node.js Connect?

To extract request HTTP headers from a request using Node.js Connect, we can use the req.headers property.

For instance, we write

const app = connect()
  .use(connect.logger('dev'))
  .use(connect.static('public'))
  .use((req, res) => {
    console.log(req.headers)
  })
  .listen(3000);

to get the request headers from the req.headers property in the 3rd middleware.

Conclusion

To extract request HTTP headers from a request using Node.js Connect, we can use the req.headers property.

Categories
JavaScript Answers

How to stop Node.js program from command line?

Sometimes, we want to stop Node.js program from command line.

In this article, we’ll look at how to stop Node.js program from command line.

How to stop Node.js program from command line?

To stop Node.js program from command line, we can use a few key combos or commands.

We can use ctrl + z to suspend the program.

We can use ctrl + c to stop the program.

Also, we can use

ps aux | grep node

to find the process ID of the node program.

Then we can use

kill 9 PROCESS_ID

to run kill with the node‘s process ID to kill node.

We can also use

killall node

to kill all instances of the node program.

Conclusion

To stop Node.js program from command line, we can use a few key combos or commands.

We can use ctrl + z to suspend the program.

We can use ctrl + c to stop the program.

Categories
JavaScript Answers

How to get data from fs.readFile?

Sometimes, we want to get data from fs.readFile.

In this article, we’ll look at how to get data from fs.readFile.

How to get data from fs.readFile?

To get data from fs.readFile, we can get it from the 2nd parameter of the callback.

For instance, we write

const fs = require('fs');

fs.readFile('./index.html', (err, data) => {
  if (err) {
    throw err;
  }

  console.log(data)
});

to call fs.readFile with the path of the file to read and a callback that has the read file.

We get the read file content from the data parameter.

Conclusion

To get data from fs.readFile, we can get it from the 2nd parameter of the callback.

Categories
JavaScript Answers

How to fix Express.js not getting static files?

Sometimes, we want to fix Express.js not getting static files.

In this article, we’ll look at how to fix Express.js not getting static files.

How to fix Express.js not getting static files?

To fix Express.js not getting static files, we call express.staric with the path to the static files.

For instance, we write

app.use("/styles", express.static(__dirname + '/styles'));

to call app.use with the path to the static files and the middleware returned by expres.static called with the path to the /styles folder in the directory.

__dirname is the path of the Express project folder.

As a result, we should be able to access the files in the styles folder with the /styles path.

Conclusion

To fix Express.js not getting static files, we call express.staric with the path to the static files.