Categories
JavaScript Answers

How to install NodeJS LTS on Windows as a local user without admin rights?

To install NodeJS LTS on Windows as a local user without admin rights, we download the zip file and then add the Node.js folder to the PATH environment variable.

To do this, we:

  1. Download the node.js LTS binary for Windows and extract it to your desired location

2.Add the path of the nodejs folder to the PATH environment variable: (Shortcut winkey+R and enter: rundll32 sysdm.cpl,EditEnvironmentVariables)

  1. Open a new command window (winkey+R and type cmd)

  2. Type node -v and npm -v to verify the installation

Categories
JavaScript Answers

How to set navigation timeout with Node.js Puppeteer?

To set navigation timeout with Node.js Puppeteer, we set the timeout option.

For instance, we write

await page.goto(url, { waitUntil: "load", timeout: 0 });

to call goto to go to the url.

We set timeout to 0 milliseconds to remove navigation timeout.

Categories
JavaScript Answers

How to include a partial with Node.js EJS?

To include a partial with Node.js EJS, we use include.

For instance, we write

<%- include('myView.ejs') %>

to use include to include the myView.ejs partial in our template.

Categories
JavaScript Answers

How to change Express view folder based on where is the file that res.render() is called with Node.js?

To change Express view folder based on where is the file that res.render() is called with Node.js, we call the res.render method with the template path.

For instance, we write

app.get("/your/path", (req, res) => {
  res.render(__dirname + "/folder/with/views/viewname");
});

to call res.render with the path to the template as in the route handler for the /your/path route.

Categories
JavaScript Answers

How to call a JSON API with Node.js?

To call a JSON API with Node.js, we install axios.

To install it, we run

npm install axios

Then we use it by writing

const { data } = await axios.post(url, {
  auth: {
    username,
    password,
  },
});

to call axios.post to make a post request to the url with the 2nd argument as the JSON payload.

Then we get the response body from the data property of the object returned by the promise.