入门:学习用Express, Postgress 和Sequelize搭建简单的REST服务器
Getting Started with Node, Express and Postgres Using Sequelize
$ mkdir -p postgres-express-react-node-tutorial/{bin,server}
$ cd postgres-express-react-node-tutorial
$ npm init -y
Install Express and a few of it's dependencies.
$ npm install --save express body-parser morgan
or
$ npm i -S express body-parser morgan
$ touch app.js
we'll need a way to restart the server every time we change something in our code. For that, we'll use the excellent nodemon npm package.
$ npm i -D nodemon
Now try running the application by executing
$ npm run start:dev
We are going to be making use of the Sequelize CLI package to bootstrap the project for us. It will also help us generate database migrations.
Let's begin by installing Sequelize CLI package.
$ npm install -g sequelize-cli
we are going to need to install the actual Sequelize package, alongside its dependencies.
$ npm install --save sequelize pg pg-hstore
With the paths defined, we will need to run the init command in order to create the specified folders and generate boilerplate code.
$ sequelize init
With the models and migrations in place, we're now ready to persist the models to the database by running the migrations.
$ sequelize db:migrate