REST API with Express.js
MySQL Example

This Example will show you how to make a REST API using Express.js and MySQL.

The database of the example contains two tables: book and user. The user will be used to authorize the REST API users. So, in the exercise you will build a login system, so that the credentials will be checked based on the user.

The book -table has these fields:

  • id_book INT primary key auto_increment
  • name VARCHAR(255)
  • author VARCHAR(255)
  • isbn VARCHAR(20)
And the user -table has these fields:
  • id_user INT primary key auto_increment
  • username VARCHAR(20) UNIQUE
  • password VARCHAR(255)

It is important that the username is UNIQUE, so that two users can not have the same username. It would have been possible to make the username to be the primary key and remove the id_user. But nowadays using this kind of id's as the primary key is very popular way.

Also it is important that the length of the password is long (at least 64 bytes), because in the example the password will be encrypted using bcrypt and the hashed password will be very long. And if the field in the database is not enough long the application will not work.

Toggle Menu