Express
Build your first API endpoint with Express
Create an Express App
api.ts
import express, { Request, Response } from 'express';
export const app = express();
// Allows cross origin requests
import cors from 'cors';
app.use(cors({ origin: true }));
app.use(express.json());
app.post('/test', (req: Request, res: Response) => {
const amount = req.body.amount;
res.status(200).send({ with_tax: amount * 7 });
});
Listen to Incoming Requests
index.ts
// Start the API with Express
import { app } from './api';
const port = process.env.PORT || 3333;
app.listen(port, () => console.log(`API available on http://localhost:${port}`));