Skip to content

Build a simple REST API with NodeJS and Express, with Typescript support baked in, all with one command!

Notifications You must be signed in to change notification settings

raj-pulapakura/build-rest-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build-rest-api

Description

A package that generates a simple Typescript Express REST server.

Usage

Run the following command to generate the server:

npx build-rest-api <dirname>

And run these commands to start the server:

cd <dirname>
npm run dev

This server uses the concurrently package to run the typescript watcher and the nodemon server at the same time.

Output

This is what the package generates for you:

<dirname>
│   .env
│   package-lock.json
|   package.json
|   tsconfig.json
│
└───node_modules
|
└───src
|   │   index.ts
|
└───dist
|   │   index.js

This is what the index.ts file looks like:

import express from "express";
import dotenv from "dotenv";
import cors from "cors";

/*
  Load environment variables
*/

dotenv.config();

/*
  Instantiate app
*/

const app = express();

/*
  Middleware
*/

app.use(express.json());

app.use(express.urlencoded({ extended: true }));

app.use(
  cors({
    origin: true,
    credentials: true,
    methods: ["GET", "POST", "OPTIONS"],
    allowedHeaders: ["Content-Type", "Set-Cookie"],
  })
);

/*
  Routes
*/

app.get("/", (req, res) => {
  res.json({ message: "The server is working!" });
});

/*
  Boot up
*/

app.listen(process.env.PORT, () => {
  console.log(
    `The server is listening at http://localhost:${process.env.PORT}`
  );
});

About

Build a simple REST API with NodeJS and Express, with Typescript support baked in, all with one command!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published