mkdir project-dir
cd project-dir
mkdir bin server
npm init -y
Express
1
2
yarn add express body-parser morgan
touch app.js
app.js 文件内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
constexpress=require('express')constlogger=require('morgan')constbodyParser=require('body-parser')constapp=express()app.use(logger('dev'))app.use(bodyParser.json())app.use(bodyParser.urlencoded({extended:false}))app.get('*',(req,res)=>{res.status(200).send({message:'Welcome to the beginning of nothingness.'})})module.exports=app
在 bin 目录中创建 www 文件
1
2
3
4
5
6
7
8
9
10
consthttp=require('http')constapp=require('../app')constport=parseInt(process.env.PORT,10)||8000app.set('port',port)constserver=http.createServer(app)server.listen(port,()=>{console.log('server running on '+port)})
为了避免每次修改代码都需要重新启动服务,我们使用 nodemon。
1
yarn add nodemon --dev
修改 package.json 创建启动服务器的命令。
1
2
3
4
5
6
...
"scripts": {
"start:dev": "nodemon ./bin/www",
"test": "echo \"Error: no test specified\" && exit 1"
},
...
exports.seed=function(knex,Promise){// Deletes ALL existing entries
returnknex('Todos').del().then(function(){// Inserts seed entries
returnknex('Todos').insert([{id:1,title:'The first todo'},{id:2,title:'The second todo'},]);});};
/db/seeds/todo_item_seed.js
1
2
3
4
5
6
7
8
9
10
11
12
exports.seed=function(knex,Promise){// Deletes ALL existing entries
returnknex('TodoItems').del().then(function(){// Inserts seed entries
returnknex('TodoItems').insert([{id:1,content:'the content 1 in todo 1',todo_id:1},{id:2,content:'the content 2 in todo 1',todo_id:1},{id:3,content:'the content 1 in todo 2',todo_id:2}]);});};
constTodo=require('../db/models').Todomodule.exports={create(req,res){returnTodo.forge({title:req.body.title}).save().then(todo=>res.status(201).send(todo)).catch(error=>res.status(400).send(error))},list(req,res){returnTodo.forge().fetchAll().then((todos)=>res.status(200).send(todos)).catch(error=>res.status(400).send(error))},retrieve(req,res){Todo.forge({id:req.params.todoId}).fetch({withRelated:['items']}).then(todo=>{if(!todo){res.status(400).send({message:'Todo Not Found'})}res.status(200).send(todo)}).catch(error=>res.status(400).send(error))},update(req,res){Todo.forge({id:req.params.todoId}).fetch().then(todo=>{if(!todo){res.status(400).send({message:'Todo Not Found'})}todo.save({title:req.body.title||todo.title}).then(todo=>{res.status(200).send(todo)})})},destroy(req,res){Todo.forge({id:req.params.todoId}).fetch().then(todo=>{if(!todo){res.status(400).send({message:'Todo Not Found'})}todo.destroy().then(()=>res.status(204).send({message:'Todo deleted successful'}))})}}
consttodosController=require('../controllers').todosconsttodoItemsController=require('../controllers').todoItemsmodule.exports=(app)=>{app.get('/api',(req,res)=>res.status(200).send({message:'Welcome to the Todo api'}))app.get('/api/todos',todosController.list)app.post('/api/todos',todosController.create)app.get('/api/todos/:todoId',todosController.retrieve)app.put('/api/todos/:todoId',todosController.update)app.delete('/api/todos/:todoId',todosController.destroy)app.post('/api/todos/:todoId/items',todoItemsController.create)app.put('/api/todos/:todoId/items/:todoItemId',todoItemsController.update)app.delete('/api/todos/:todoId/items/:todoItemId',todoItemsController.destroy)}