This is an easy, basic and raw example of HOW to implement an API with Node, TS, Express and PostgreSQL to authenticate and authorize with jsonwebtoken.
- Node 12+
- NPM
- PostgreSQL
- db-migrate package as a global dependency
Note: Other libraries will be installed as part of the project with npm install
Create at the root level of the project a .env
file following the structure of .env-sample
Update the file database.json
with the proper information.
npm install
createdb test_db
createdb test_db_test
npm run migrate:up
npm run dev
npm run build
npm start
npm test
npm run lint
If the error occurs at the controller layer
(or the logic inside the model), I throw the error passing the error object to the express handler function.
node-typescript-express-postgresql-authentication-authorization/src/models/user.ts
throw new Error(`Cannot get users, ${err}`);
If the error occurs at the handler function
...
-
If the error happens due to throwing an error in the controller (or model logic) I return:
res.status(500).json({ message: 'Something went wrong!' });
Why I decided to not pass the error to the client? For security concerns and because the client should just care about its interaction with the API, not with the underlying servers (for example, the database). -
For other errors, aka, errors directly related to the express handler, like
bad request
,not found
we respond with the proper statusCode and message.
- Extended version of Udacity's JSFSN User Project