Let's Build Project Management System Pt. 3
Inside the backend folder, let’s create an app.js file and write some base code to test our setup.
touch app.js
We will use the modern import...from syntax. We’ll update the necessary configuration in the package.json file shortly.
import express from "express"; const app = express();
Now, let’s define a /ping route that returns "pong" as a JSON response.
import express from "express"; const app = express(); app.get("/ping", (req, res) => { return res.json({ data: "pong" }); });
ðĄ Tip: Always use return when you want to stop the execution of a function after sending a response.
We will use port 3000 because the React application created using Vite runs on port 5173 by default.
import express from "express"; const app = express(); app.get("/ping", (req, res) => { return res.json({ data: "pong" }); }); const PORT = process.env.PORT || 3000;
Finally, let’s start the server and print the URL for future reference and debugging.
import express from "express"; const app = express(); app.get("/ping", (req, res) => { return res.json({ data: "pong" }); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`http://localhost:${PORT}`); });
We need to make the following changes in the package.json file:
- Change
"type"from"commonjs"to"module"to support theimport...fromsyntax. - Change
"main"fromindex.jstoapp.js.
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"compression": "1.8.1",
"cors": "2.8.6",
"express": "5.2.1",
"helmet": "8.1.0",
"morgan": "1.10.1",
"postgres": "3.4.8"
}
}
I almost forgot — we also need nodemon to automatically restart the server when files change during development.
ðĄ Tip: This is an example of a non-perfect setup. You will always miss something — and that’s completely fine.
Install nodemon as a development dependency:
npm i -E -D nodemon
Now, let’s add two scripts to package.json:
dev→ runsnodemon app.jsstart→ runsnode app.js
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"dev": "nodemon app.js",
"start": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"compression": "1.8.1",
"cors": "2.8.6",
"express": "5.2.1",
"helmet": "8.1.0",
"morgan": "1.10.1",
"postgres": "3.4.8"
},
"devDependencies": {
"nodemon": "3.1.11"
}
}
With these changes, we are ready to run the application. Both scripts will start the server, but during development, we usually use:
npm run dev
Now open your browser and visit http://localhost:3000/ping. You should see { "data": "pong" } And yup — it’s working! ð If everything works on your side, you’re ready to head over to the next article.
---