Basic express.js Development Server Setup

To set up a server using express.js, add the following code to src/index.js:

// index.js
// This is the main entry point of our application

const express = require('express');
const app = express();
const port = process.env.PORT || 4000;

app.get('/', (req, res) => res.send('Hello World!!!'));

app.listen(port, () =>
  console.log(`Server running at http://localhost:${port}`)
);

Start the server, and listen on port 4000, with this command:

node src/index.js

You can now browse to http://localhost:4000, to see Hello World!!!

Any change to the code, will require a restart to the server, and a browser refresh.

To automate this, add the following to your package.json:

"dev": "nodemon src/index.js",

You can now use node to monitor for any changes, with this command:

npm run dev

How to Update npm

Node Package Manager (npm) always seems to have an update available. Fortunately, it’s quick and easy to update.

Display the current npm version, update npm, and check the version again:

npm --version
npm install -g npm
npm --version

Here’s the link to the official npm docs for the update.

You can also download the latest node.js if needed here. It’s recommended to use the LTS version, which has been tested with npm.

Working with Big Numbers in JavaScript

Need to work with a really big number in JavaScript? Larger than a normal int? BigInt is your answer!

Say, for example, you were trying to solve the “A Very Big Sum” problem on HackerRank. The code is simple with BigInt():

function aVeryBigSum(ar) {
    // Write your code here
    let sum = 0n;

    for (let i = 0; i < ar.length; i++) {
        sum = sum + BigInt(ar[i]);
    }
    return sum;
}

You can find detail on Mozilla and W3docs.