Skip to main content

Modern API development(Part 2): Initialize Server

· 5 min read
Mitch Chimwemwe Chanza
note

Refer to the earlier post - part 1 , to familiarize yourself with the configuration outlined there. This will enable you to continue with the instructions provided in this guide.

Quick Overview

in this part we are going to implement the following:

  • Install additional dependencies
  • Additional project configurations
  • Setup minimal server using express
  • Test the server

Initial Server configuration

# create a src folder inside api folder
mkdir -p api/src
#create main typescript file
touch api/src/main.ts
# lets change directory to api
cd api
# install additional api dependencies
pnpm add cors express dotenv
# this will install three dependencies in devdependencies
pnpm add -D @types/{node,cors,express} nodemon

info

Important: Additional configuration

It's worth mentioning that when using pnpm, we have the ability to set filters in order to run scripts globally in the project. Let's go ahead and set up some global scripts with filters. We also need to trim the typescript compile configuration file tsconfig.json.

{
"scripts" {
"api":"pnpm --filter api"
}
// ... rest of the configuration
}

Adding additional settings to our project is optional but can help reduce clutter in your script. Specifically, we need to configure nodemon to run .ts files without waiting for compilation. This can be achieved by using the ts-node module. Let's take a look at the configuration.

# we need first to install ts-node
pnpm api add -D ts-node

Following the installation of ts-node, the next step involves configuring nodemon to operate in conjunction with ts-node. To achieve this, it is necessary to establish a nodemon configuration file, nodemon.json, located at the root of our project. Additionally, we must modify our dev script to execute nodemon while adhering to the specified configuration.

{
"watch": ["src","graphql","hasura","prisma,tsconfig.json,package.json,.env"],
"ext": "ts,graphql,json,prisma,js,env",
"ignore": ["**/*.spec.ts","**/*.spec.graphql","**/*.spec.json","**/*.spec.js"],
"execMap": {
"ts": "ts-node ./src/main.ts"
},

"exec": "ts-node ./src/main.ts"
}

Now that we have prepared the environment for our server to run smoothly, it's time to set up the server and test it. we need to create two files, the main entery file for our api api/src/main.ts and environmental variables file api/.env

import express, { json } from "express";
import cors from "cors";
import dotenv from "dotenv";
dotenv.config();
const env = process.env;
const app = express();
// Initialse base router
const router = express.Router();
// Set initial route
router.get("/", (_req, res) => {
res.send({ message: "Monorepo API Configured!", success: true});
});
// Set v1/api endpoint
app.use("/v1/api", router);
// configure cors. the delimeter here can be anything that you have used in your .env file. for my example here am using comma to separate the urls.
app.use(cors({ origin: env.ALLOWED_ORIGINS?.split(",") }));
// enable json serialization
app.use(json());
// start server
app.listen(env.PORT ? +env.PORT : 9000, () => {
console.log(`Server started on http://localhost:${env.PORT}/v1/api`);
})

Lets run the server now.

pnpm api dev
# our server shpuld be up and running on http://localhost:9100 or http://localhost:9000 depending on the PORT set in .env file

Am glad you have made it this far. we have more to cover in this series. proceed to Part 3 to get started.