This is an Express service that provides authorization functionality and includes separate folders for users and products. It also uses Sequelize ORM with SQLite as the database, along with the JSON Web Token (JWT) and AJV libraries.
index.js
: The main entry point of the application.config.js
: Contains configuration files for the application.authorization
controllers
: Controller files for authentication endpoints.schemas
: JSON Schemas against which the body of various routes will be validated.routes.js
: Registers all the authentication routes.
products
controllers
: Controller files for product master CRUD endpoints.schemas
: JSON Schemas against which the body of various routes will be validated.routes.js
: Registers all the product CRUD routes.
users
controllers
: Controller files for user master CRUD endpoints.schemas
: JSON Schemas against which the body of various routes will be validated.routes.js
: Registers all the user CRUD routes.
common
middlewares
: Various middlewares that can be used in various routes like (isAuthenticated, CheckPermissions etc.)models
: Sequelise models for the Product and User Tables
storage
: Local storage, that stores all the SQLite tables.
Before running the application, make sure you have the following installed:
- NodeJS (v18)
- NPM (v9)
- Clone the repository:
git clone git@github.com:gabvillacis/e-commerce-store-express.git
- Install the dependencies:
npm install
-
Express Express.js is a web application framework for Node.js, designed for building web applications and APIs
-
const privateKey = "a private key" jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256' }, function(err, token) { console.log(token); });
-
Sequelize Is a modern TypeScript and Node.js ORM for Oracle, Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, relations, eager and lazy loading, read replication and more.
Define model
import { S 8000 equelize, DataTypes } from 'sequelize'; const sequelize = new Sequelize('sqlite::memory:'); //const sequelize = new Sequelize({ //dialect: "sqlite", //storage: "./storage/data.db", // Path to the file that will store the SQLite DB. //}); const User = sequelize.define('User', { username: DataTypes.STRING, birthday: DataTypes.DATE, });
Persit and query
const jane = await User.create({ username: 'janedoe', birthday: new Date(1980, 6, 20), }); const users = await User.findAll();
-
AJV JSON schema validator, we use it to validate JSON input params.
// or ESM/TypeScript import import Ajv from "ajv" // Node.js require: const Ajv = require("ajv") const ajv = new Ajv() // options can be passed, e.g. {allErrors: true} const schema = { type: "object", properties: { foo: {type: "integer"}, bar: {type: "string"}, }, required: ["foo"], additionalProperties: false, } const data = { foo: 1, bar: "abc", } const validate = ajv.compile(schema) const valid = validate(data) if (!valid) console.log(validate.errors)
-
Morgan A logger middleware function
const app = Express(); ... app.use(morgan("tiny"));
-
CORS A libary to for express middleware to config CORS
- Enable CORS for all request
var cors = require('cors') var app = express() app.use(cors())
- Enable for signle routes
- Block request
- ...
To start the service, run the following command:
npm start
curl --location 'localhost:3000/signup' \
--data-raw '{
"firstName": "Dinq",
"lastName": "Truong",
"email": "dinqtruong@gmail.com",
"username": "dinqtruong",
"password": "Qwertyuiop@!#$1091",
"role": "admin",
"age": 18
}'
curl --location 'localhost:3000/login' \
--data '{
"username": "dinqtruong",
"password": "Qwertyuiop@!#$1091"
}'
curl --location 'localhost:3000/user' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsInVzZXJuYW1lIjoicXVhbmd0cnVvbmdkLWFkbWluIiwiaWF0IjoxNzA2MDY3NjEzLCJleHAiOjE3MDYwNzEyMTN9.UMs0wF5mSRH0skPDFfH_mREVQUgBxAhk4yroHmk8y28'
curl --location 'localhost:3000/product' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsInVzZXJuYW1lIjoicXVhbmd0cnVvbmdkLWFkbWluIiwiaWF0IjoxNzA2MDY3NjEzLCJleHAiOjE3MDYwNzEyMTN9.UMs0wF5mSRH0skPDFfH_mREVQUgBxAhk4yroHmk8y28'
This project is licensed under the MIT License.
https://blog.postman.com/how-to-create-a-rest-api-with-node-js-and-express/