This boilerplate includes the setup for a Node.js application using Express, MySQL with Sequelize ORM, and Winston for logging.
- Node.js: >= v18.16.1
- Framework: Express
- Database: MySQL
- ORM: Sequelize
- Logger: Winston
-
Copy the example environment file:
cp .envexample .env
-
Install dependencies:
npm install
-
For development:
npm run watch
-
For production:
npm run build
You can configure the port on which your application runs. Check the .env
file:
APP_PORT=5000
Update the .env
file according to your database configuration:
# DB CONFIGURATION
DB_CONNECTION=mysql
DB_NAME=sts
DB_USERNAME=root
DB_PASSWORD=secret
DB_HOST=localhost
DB_PORT=33060
npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string
npx sequelize-cli migration:generate --name migration-skeleton
npx sequelize-cli db:migrate --to XXXXXXXXXXXXXX-create-posts.js
npx sequelize-cli db:migrate
npx sequelize-cli db:migrate:undo
npx sequelize-cli db:migrate:undo:all
npx sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js
npx sequelize-cli seed:generate --name demo-user
npx sequelize-cli db:seed:all
npx sequelize-cli db:seed:undo
npx sequelize-cli db:seed:undo:all
npx sequelize-cli db:seed:undo --seed name-of-seed-as-in-data
You can modify your log configuration by updating the .env file:
LOG_LEVEL='info'
LOG_STORAGE_PATH='src/storage/logs'
Log levels:
error: 0
warn: 1
info: 2
http: 3
verbose: 4
debug: 5
silly: 6
Example of writing logs in your application:
logger.info('userProfile', 200, 'User profile fetched successfully', { user: req.user });
logger.warn('This is a warning message');
logger.error('This is an error message');
logger.debug('This is a debug message');