In this article, you’re gonna learn how to authenticate api’s. We’re gonna use typescript and mongo fot this.
We’re going to use the yarn package manager. Npm is also good. Init the repository with git init and yarn init. Make src diretory. It will be typescript project. So be sure to install all types packages.
# Install dev dependencies
yarn add typescript @types/node ts-node nodemon --save-dev
# Init tsc config
yarn tsc --init
Add a script command to the package.json file so that we can start coding in ts with auto restarting the server.
"scripts": {
"dev": "nodemon src/index.ts"
}
Let’s run our server to test the setup npm run dev
.
Let’s add formating, linting and testing. This will be a TDD approach.
Add prettier and eslint for formating and linting.
yarn add -D @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint prettier
yarn init @eslint/config
Add prettier and eslint script
"format": "prettier --write \"src/**/*.{ts, tsx}\""
"lint": "eslint . --ext .ts"
Install express and types express
yarn add express @types/express
Then create a server.ts file with these content.
import express, {Express} from "express"
const app: Express = express()
export default app
Then add these in the index file
import app from "./server"
const PORT: number = 8000 || process.env.PORT
app.listen(PORT, () => {
console.log(`Server running on port ${PORT} ...`)
})
Then run the server with
yarn run dev
When adding .env file be sure to use dotenv package.
yarn add dotenv
import dotenv
dotenv.config()
dotenv will have string as input. For getting types from dotenv into your typescript project. use ~import “dotenv/config”~ at the top of your file.
typescript non null assertion operator !. Which means the variable will not be undefined.
It took me a long time to fix this problem but define the mongouri carefully. It’s the main culprit.
First we’re gonna setup our development environment. typescript
for the language itself is a developer tool. It doesn’t go to production. Eslint
for linting, prettier
for automatic formating, nodemon
and ts-node
for compiling and running typescript. For testing we have jest
and supertest
. You can install them now or later as we continue to progress.
Time to add testing to our project. Install supertest and jest with their @types.
install ts-jest and init it within proejct.
If you get module error use ts-jest as preset . Search for jest for typescript.
Without the body parser we can’t access req.body features. If you want to parse json data you need to specify it. bodyparser.json() bodyparser.urlencoded({extended:true})
app cors
typescript error if expression expected somewhere near export default and connst . Declare const and declare that export in a separate line.
Added middleware. Signup and login
How to get the objct id from mongodb
d = findOne()._id
d.toString()
GET http://localhost:8000/secret
POST http://localhost:8000/signup
Content-Type: application/json
{"userName": "yor1", "password": "yor"}
POST http://localhost:8000/login/
Content-Type: application/json
{"userName": "yor1", "password": "yor"}
:bearer = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY0OTUwNjIzNTk3MTc4MDU1Njc3NDYyMiIsInVzZXJuYW1lIjoieW9yMSIsImlhdCI6MTY4NzQ4ODA2Nn0.-sMtkFAYI3oxirAOKMrQv3QXBemHAPIcGXLcyWgpM20
Authorization: :bearer
POST http://localhost:8000/secret/
Content-Type: application/json/