Last time nginx served a static page. Today our website becomes a real Node.js app. We install Node.js on our Ubuntu server on AWS Lightsail, build an Express server that serves the website and saves every contact form message to a file, and keep it running 24/7 with pm2, even after a reboot.
- Node.js and npm on the server
- An Express app that serves the website
- A Contact Us form whose messages are saved to
contacts.txt - pm2 keeping the app running, and starting it again after a reboot
Install Node.js and npm
Node.js runs JavaScript on the server, and npm installs its packages. Both come from the Ubuntu repositories:
sudo apt install -y nodejs npm
node -v && npm -v
Create the Node.js project
A project folder, with a public folder inside for the website files:
mkdir -p ~/fineshot-app/public && cd ~/fineshot-app
cp -r ~/fineshot_social/* public/
npm init -y
npm install express
ls
npm init -y creates package.json, and npm install express adds the Express web framework. If you only need a plain static site, you don’t need Node.js at all: episode 4 hosts it with nginx alone.
Write the Express server
Create server.js in the project folder (vim server.js):
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 3000;
// read form data and serve the static website
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// contact form: save every message in contacts.txt
app.post('/contact', (req, res) => {
const { name, email, message } = req.body;
const entry = { name, email, message, time: new Date().toISOString() };
fs.appendFileSync(path.join(__dirname, 'contacts.txt'), JSON.stringify(entry) + '\n');
res.send('<h2>Thank you! Your message has been saved.</h2><a href="/">Back to home</a>');
});
app.listen(PORT, () => console.log('FineShot app running on port ' + PORT));
express.urlencodedreads the data a form sends.express.staticserves everything in thepublicfolder: our website.- The
POST /contactroute takes the name, email and message, adds a timestamp, and appends it as one line of JSON tocontacts.txt. app.listenstarts the server on port 3000.
Add a Contact Us page
Create public/contact.html (vim public/contact.html):
<!doctype html>
<html>
<head>
<title>Contact Us - FineShot.AI</title>
<style>
body { font-family: sans-serif; background: #111; color: #eee; display: flex; justify-content: center; padding-top: 60px; }
form { display: flex; flex-direction: column; gap: 12px; width: 360px; }
input, textarea, button { padding: 10px; border-radius: 6px; border: 1px solid #444; font-size: 15px; }
button { background: #e53935; color: #fff; border: none; cursor: pointer; }
</style>
</head>
<body>
<form action="/contact" method="POST">
<h2>Contact Us</h2>
<input name="name" placeholder="Your name" required>
<input name="email" type="email" placeholder="Your email" required>
<textarea name="message" rows="5" placeholder="Your message" required></textarea>
<button type="submit">Send</button>
</form>
</body>
</html>
It’s a normal HTML form. Its action is /contact with method="POST", which is exactly the route we just wrote. Check both pages are in place with ls public.
Run the app with pm2
If you start the app with node server.js, it stops when you log out. pm2 is a process manager that keeps it running in the background and restarts it if it crashes:
sudo npm install -g pm2
pm2 start server.js --name fineshot
curl -s http://localhost:3000 | head -3
pm2 shows the app as online, and curl on port 3000 returns our HTML.
Start the app on every reboot
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u ubuntu --hp /home/ubuntu
pm2 save
pm2 startup creates a systemd service that starts pm2 when the server boots, and pm2 save stores the current list of apps so they come back automatically.
Open port 3000 in the Lightsail firewall
To reach port 3000 from the internet, open it in Lightsail: Networking → IPv4 firewall → Add rule, a custom TCP rule for port 3000.
This is temporary, only for this demo. Leaving an app port open to the world is not how a live site should run. The right way is to put nginx in front of Node.js as a reverse proxy and close port 3000 again, which is the next step on the route.
Node.js is listening on port 3000
pm2 status
sudo ss -ltnp | grep node
pm2 status shows our app online, and ss confirms Node.js is listening on port 3000.
Open the app on port 3000
Open http://YOUR_SERVER_IP:3000 and then http://YOUR_SERVER_IP:3000/contact.html. The browser now talks to Node.js directly, with no nginx in between.
Send a contact form message
Send the same request the Send button makes, then check it was saved:
curl -s -X POST http://localhost:3000/contact -d 'name=Demo User&email=demo@example.com&message=Hello from the FineShot contact form'; echo
cat contacts.txt
pm2 logs fineshot --lines 5 --nostream
Express answers with its thank-you message, contacts.txt holds the message with a timestamp, and the pm2 logs show the app running on port 3000.
If it doesn’t work
Error: listen EADDRINUSE: address already in use :::3000. Something else, often an earlier copy of the app, is already on port 3000. Find it with sudo ss -ltnp | grep 3000, or check pm2 status for a duplicate and remove it with pm2 delete.
The browser can’t open port 3000. The Lightsail firewall rule is missing, or the app isn’t online: check pm2 status and pm2 logs fineshot.
The app is gone after a reboot. Run the pm2 startup line again, then pm2 save while the app is running.
Next
Our site is now a Node.js app with a working contact form, managed by pm2. But port 3000 is open to the world. Next, nginx moves in front as a reverse proxy, and port 3000 closes again.
