Make API REST on node.js
Install pixelrest with npm:
npm install --save pixelrest
Generate a project pixelrest:
pixelrest-new
Set your node project in ES module mode, add the following property to your package.json file:
"type": "module"Pixelrest supports MySQL/MariaDB and PostgreSQL.
Create a MySQL/MariaDB database (InnoDB).
Create a PostgreSQL database.
Create file secret.js in app/config with your database credentials and JWT secret.
MySQL example:
export const DB_CREDENTIALS = {
DATABASE: 'mydatabase',
HOST: 'localhost',
PORT: 3306,
USERNAME: 'root',
PASSWORD: 'password'
};
export const JWT = {
SECRET: 'mysecret',
EXPIRES_IN: 14400
};PostgreSQL example:
export const DB_CREDENTIALS = {
DATABASE: 'mydatabase',
HOST: 'localhost',
PORT: 5432,
USERNAME: 'postgres',
PASSWORD: 'password'
};
export const JWT = {
SECRET: 'mysecret',
EXPIRES_IN: 14400
};Prepare your database with the script:
node ./app/scripts/prepareDatabase.js
Start your server:
npm start
Test your API REST with swagger:
http://localhost:1338/api-docs
Before testing the documents service, don't forget to create the documents directory in your project.
| Command | Description |
|---|---|
npm start |
Start the server with nodemon |
npm test |
Run tests with Vitest |
npm run test:watch |
Run tests in watch mode |
npm run lint |
Run ESLint |
npm run prepare-file |
Prepare database tables |
The pixelrest-new command generates a ready-to-use project structure with example services, repositories, and configuration files.
npx pixelrest-new
Never push secret.js on your git. You should add it to your .gitignore!