Skip to content

Instantly share code, notes, and snippets.

@fractalliter
Last active April 28, 2024 19:31
Show Gist options
  • Save fractalliter/156cc79beff1d3a6e843494e919e67bb to your computer and use it in GitHub Desktop.
Save fractalliter/156cc79beff1d3a6e843494e919e67bb to your computer and use it in GitHub Desktop.
How to create Systemd service with Typescript and Express

Description

Systemd is a suite of basic building blocks for a Linux system. It provides a system and service manager that runs as PID 1 and starts the rest of the system. There are cases that you want to run a script or job on a Linux server and keep track of the logs without an external library and also get the benfit of seamless control over the application lifecycle on the server. Systemd provides all of this out of the box. In this simple example I tried to demonstrate the use case with a simple Express hello world server that is developed in Typescript and TS-Node.

Prerequisites

  • Nodejs
  • NPM

How to create systemd service

You need to install Node in your Linux server and then install ts-node package:

# Or globally with TypeScript.
npm install -g typescript
npm install -g ts-node

Create a directory at /usr/local/ for your app. you may need admin permission:

mkdir /usr/local/hello-world

Then change your direcotry to:

cd /usr/local/hello-world

You need to initialize your node project and install express library:

npm init -y

npm i express
npm i -D @types/express

you need to create index.ts file and copy/paste the content of the file into it. we are set with the script but we also need to create systemd service:

touch /etc/systemd/system/hello-world.service

and then copy/paste the content of hello-server.service file into it.

We are also set with systemd service configruation.

How to run

You are now able to execute the server under systemd services and control the status with systemd commands.

systemctl daemon-reload
systemctl enable hello-world.service
systemctl start hello-world.service

How to see logs

We can easily check logs for the service in real-time via journalctl:

journalctl -u hello-world.service -f

You should see something like bellow if you send curl localhost:3000/elias?github=fractalliter:

Apr 28 20:36:44 user-something systemd[1]: Started Hello world server.
Apr 28 20:36:46 user-something ts-node[21800]: Server running at PORT:  3000
Apr 28 20:36:57 user-something ts-node[21800]: request received param elias that queries {"github":"fractalliter"}

JournalCTL enables us to query the logs between dates and for specific log level like error or info.

[Unit]
Description=Hello world server
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=on-failure
RestartSec=1
ExecStart=/usr/bin/ts-node /usr/local/hello-world/index.ts
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
#!/usr/bin/ts-node
import express, { Request, Response } from "express";
const app = express();
app.use(express.json())
const PORT = process.env.PORT;
app.get("/:name", (request: Request, response: Response) => {
console.log(`request received param ${request.params.name} that queries ${JSON.stringify(request.query)}`);
response.status(200).send("Hello World");
});
app.listen(PORT, () => {
console.log("Server running at PORT: ", PORT);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment