Categories
JavaScript Answers

How to render basic HTML view with Express?

Sometimes, we want to render basic HTML view with Express.

In this article, we’ll look at how to render basic HTML view with Express.

How to render basic HTML view with Express?

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.

For instance, we write

const express = require('express');
const app = express.createServer();

app.set("view options", {
  layout: false
});
app.use(express.static(__dirname + '/public'));

app.get('/', (req, res) => {
  res.render('index.html');
});

app.listen(8080, '127.0.0.1')

to call express.static with the static folder path in our project to serve that as the static folder.

Then we call res.render with a path to a file in the static folder to render that as the view content.

Conclusion

To render basic HTML view with Express, we call app.use with express.static to serve a static folder.

Categories
JavaScript Answers

How to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js?

Sometimes, we want to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js.

In this article, we’ll look at how to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js.

How to format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js?

To format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js, we can use the toISOString method.

For instance, we write

new Date().toISOString()
  .replace(/T/, ' ')
  .replace(/\..+/, '')

to call toISOString on a Date instance.

Then we replace the T and the periods with a space and empty string respectively.

Conclusion

To format a UTC date as a ‘YYYY-MM-DD hh:mm:s’ string using Node.js, we can use the toISOString method.

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.