Skip to content

shuikang/Express-REST-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express-REST-API

Prepare for code

  1. Install nodemon with following command
npm i -g nodemon
  1. Copy .env.example to .env with following command
cp .env.example .env
  1. Add value to field without doube-quote("") or single-quote('') in file .env

For Migration & Seeder

  1. Install knex cli with following command
npm i -g knex
  1. Go to database directory with following command
cd database
  1. Create migration with following command
knex migrate:make migrate_table_name
  1. Code create table with following example in function of exports.up and see other attribute in Knex
return knex.schema.createTable('table_name', function (table) {
    table.increments('id')
    table.string('filed')
})
  1. Migrate table with following command
knex migrate:latest
  1. Create seed with following command
knex seed:make seed_name
  1. Change data in seed.

For this project, it already install faker for mock data.

You can copy the following code and paste it at the top of seed file.

You can read how to use it in facker

const faker = require('faker')
  1. Run seed with following command
knex seed:run

Project Structure

  • 📁 app
    • this directory will contain all of the application logic.
    • 📁 controllers
      • this is where the controllers are defined.
      • all controllers have to do with CRUD actions on Models.
    • 📁 lib
      • this is where the configuration's libraries are defined.
    • 📁 models
      • this is where the models are defined.
      • all models have to do CRUD actions.
    • 📁 routes
      • this is where the application's routes are defined.
      • 📄 index.js
        • this is main route file for collect any route file in routes folder
  • 📁 database
    • this directory will contain files and folder for build API's database.
    • 📁 migrations
      • the migrations is automatic generated by knex command for create migrate file.
    • 📁 seeds
      • the seeds is automatic generated by knex command for create seed file.
    • 📄 knexfile.js
      • this contain database configuration.
  • 📄 index.js
    • this is main project file.
    • it contain configuration of express js.
  • 📄 package.json
    • this contain node configuration include dependencies.

About

Template of express restful api with mysql

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published