Bootstrap FreeKB - Node.js - Getting Started with NodeJS Express
Node.js - Getting Started with NodeJS Express

Updated:   |  Node.js articles

In this walkthrough, I am on a Linux system. Let's create a directory.

mkdir /tmp/foo

 

And move into the directory.

cd /tmp/foo

 

This issues you have the npm (Node.js Package Manager) CLI installed on your system. If not, check out my article FreeKB - Node.js - Install Node.js on Linux. Let's use the npm install command to install the express package.

npm install express

 

This will create the package.json file. Your package.json file should contain something like this.

{
  "dependencies": {
    "express": "^4.19.2"
  }
}

 

Let's create a file named app.js.

touch app.js

 

And let's add the following to app.js. Notice this requires express.

const express = require("express")
const app = express()
const timestamp = () => Date()

app.get('/', (req, res) => {
  console.log(`${timestamp()} Hello Console`)
  res.send(`${timestamp()} Hello World`)
})

app.listen(12345)
console.log(`Node.js app is up and running!`);

 

Be aware that if your are using a Node.js module instead of CommonJS, you will have "type": "module" in package.json. In this scenario, you will use import instead of require.

import express from "express";

 

Let's use the node CLI to run app.js and if all goes according the plan the following should be displayed.

~]$ node app.js &
Node.js Express app is up and running!

 

Let's say firewalld is running on your Linux system.

~]$ sudo systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
     Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-07-24 02:40:19 CDT; 7 months 29 days ago

 

Let's update firewalld to allow connections on port 12345. I did not use the --permanent flag because I don't want this to be a permanent change to firewalld since port 12345 is just the port I'm using for testing purposes.

~]$ sudo firewall-cmd --add-port=12345/tcp
success
~]$ sudo firewall-cmd --list-port
12345/tcp

 

And when you go to http://<hostname or IP address>:<port> you should get something like this. Nice!

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 92aad7 in the box below so that we can be sure you are a human.