Express API
Express is a minimal and flexible Node.js web application framework. We will be using it to build our API.
Let's create an endpoint to get an emoji.
Here's the list of what we have to do:
Delete
Emoji.tsandEmoji.spec.ts.Install
expressCreate a
routersfolder to keep our files organisedCreate a new file
/routers/emojiCreate a new file
app.tsCreate a new file
index.ts(our main file)Update our config with
shared.tsand.envto addport.To simplify imports, add
index.tsexporting all the relevant files to each folder.
We will create the following file structure
.
βββ app.ts
βββ index.ts
βββ routers
Β Β βββ Emoji
| βββ index.spec.ts
| βββ index.ts
βββ index.tsTo keep things clean, each top-level endpoint will have a router.
The router defines the GET, POST, PUT and DELETE.
We can test any method by mocking request and response using jest.fn().mockReturnValue().
We need a way to bootstrap our app (set containers, connect to the database, ...) and create an express app.
Our main file requires it and starts the server:
Note:
sharedConfigand.envneeds to be updated to addport.
Last updated
Was this helpful?