Start with a Backend App Development
For our event scheduling app, we will need to create the backend connectivity application and expose APIs from the backend application. To do this application, we are going to use NodeJs, ExpressJS and Postgres as our DB.
Let us start creating our node application. We will be adding features as we go along. For a completed application, get the codebase from github devops-journey-backend.
Step 1: Create a vanilla Nodejs + express app
Create a directory where you need to have the code.
$ mkdir devops-journey-backend$ cd devops-journey-backend$ npm init --yes //initialize your node package manager$ npm i express$ npm i --save-dev nodemon dotenv//Install express, nodemon and dot env as devDependencies
This will create a base nodejs project. Open this project in an editor.
Right now, we don't have any files except the package.json and package-lock.json files.
Step 2: Create an app.js file and add the following code to the file.
const express = require("express");const app = express();app.use(express.json());const port = 3000;app.get("/", (request, response) => {response.json({ info: "Node.js, Express, and Postgres API" });});app.listen(port, () => {console.log(`Server running on ${port}...`);});
Run the node server using this command.
node app.js
The server will start listening into the port 3000 and print the log in the console. This is a direct way of calling a file to run in node environment. But directly running this command is not helpful for us to use this app in different environments. To overcome that, let us create a script entry.
Update scripts object in package.json with this command.
"start": "NODE_ENV=dev node app.js",
By adding this command, we are enabling node to use DEV
environment and start the script for DEV environment configuration.
To start the node server, run this command
npm run start
Now our node server is running in port 3000.
We have added a /
route to the application. If you open up a browser and hit http://localhost:3000
you should see the info message printed on the screen.