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
module.exports=(sequelize,DataTypes)=>{constTodo=sequelize.define('Todo',{title:{type:DataTypes.STRING,allowNull:false,}},{classMethods:{associate:(models)=>{// associations can be defined here
Todo.hasMany(models.TodoItem,{foreignKey:'todoId',as:'todoItems',})}}});returnTodo;};
module.exports=(sequelize,DataTypes)=>{constTodoItem=sequelize.define('TodoItem',{content:{type:DataTypes.STRING,allowNull:false,},complete:{type:DataTypes.BOOLEAN,defaultValue:false}},{classMethods:{associate:(models)=>{// associations can be defined here
TodoItem.belongsTo(models.Todo,{foreignKey:'todoId',onDelete:'CASCADE'})}}});returnTodoItem;};
consttodosController=require('../controllers').todosmodule.exports=(app)=>{app.get('/api',(req,res)=>res.status(200).send({message:'Welcome to the Todos API'}))app.post('/api/todos',todosController.create)}
constTodo=require('../models').TodoconstTodoItem=require('../models').TodoItemmodule.exports={create(req,res){...},list(req,res){...},retrieve(req,res){returnTodo.findById(req.params.todoId,{include:[{model:TodoItem,as:'todoItems'}]}).then(todo=>{if(!todo){returnres.status(400).send({message:'Todo Not Found'})}returnres.status(200).send(todo)}).catch(error=>res.status(400).send(error))},}
update(req,res){returnTodo.findById(req.params.todoId,{include:[{model:TodoItem,as:'todoItems'}]}).then(todo=>{if(!todo){returnres.status(400).send({message:'Todo Not Found'})}returntodo.update({title:req.body.title||todo.title,}).then(()=>res.status(200).send(todo)).catch((error)=>res.status(400).send(error))})},
destroy(req,res){returnTodo.findById(req.params.todoId).then(todo=>{if(!todo){returnres.status(400).send({message:'Todo Not Found'})}returntodo.destroy().then(()=>res.status(204).send()).catch(error=>res.status(400).send(error))}).catch(error=>res.status(400).send(error))}
constTodoItem=require('../models').TodoItemmodule.exports={create(req,res){returnTodoItem.create({content:req.body.content,todoId:req.params.todoId,}).then(todoItem=>res.status(201).send(todoItem)).catch(error=>res.status(400).send(error))},update(req,res){returnTodoItem.find({where:{id:req.params.todoItemId,},}).then(todoItem=>{if(!todoItem){returnres.status(400).send({message:'TodoItem Not Found'})}returntodoItem.update({content:req.body.content||todoItem.content,complete:req.body.complete||todoItem.complete}).then(updatedTodoItem=>res.status(200).send(updatedTodoItem)).catch(error=>res.status(400).send(error))}).catch(error=>res.status(400).send(error));},destroy(req,res){returnTodoItem.find({where:{id:req.params.todoItemId,},}).then(todoItem=>{if(!todoItem){returnres.status(400).send({message:'TodoItem Not Found'})}returntodoItem.destroy().then(()=>res.status(204).send()).catch(error=>res.status(400).send(error))}).catch(error=>res.status(400).send(error))}}
consttodosController=require('../controllers').todosconsttodoItemsController=require('../controllers').todoItemsmodule.exports=(app)=>{app.get('/api',(req,res)=>res.status(200).send({message:'Welcome to the Todos API'}))app.post('/api/todos',todosController.create)app.get('/api/todos',todosController.list)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)app.all('/api/todos/:todoId/items',(req,res)=>{res.status(405).send({message:'Method Not Allowed'})})}