项目目录结构如下
.
|-- README.md
|-- cloudbaserc.json
|-- functions
| `-- node-app
| |-- app.js
| |-- index.js
| |-- package-lock.json
| |-- package.json
| |-- router
| | |-- index.js
| | `-- todo.js
| `-- www.js
`-- package.json
// todoList\functions\node-app\index.js
const serverless = require('serverless-http')
const app = require('./app')
const handler = serverless(app)
exports.main = async (event, context) => {
const res = await handler(event, context)
return res
}
// todoList\functions\node-app\www.js
const app = require('./app')
app.listen(3000, () => {
console.log('服务运行于http://127.0.0.1:3000');
})
// todoList\functions\node-app\app.js
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({
extended: false
}))
const indexRouter = require('./router/index')
const todoRouter = require('./router/todo')
app.use('/', indexRouter)
app.use('/todo', todoRouter)
module.exports = app
// todoList\functions\node-app\router\index.js
const express = require('express')
const router = express.Router()
router.get('/', (req, res) => {
res.send('index router')
})
module.exports = router
// todoList\functions\node-app\router\todo.js
const express = require('express')
const router = express.Router()
/** 获取任务 */
router.get('/', (req, res) => {
res.send('todo router 查')
})
/** 添加任务 */
router.post('/', (req, res) => {
res.send('todo 增')
})
/** 修改任务 */
router.put('/', (req, res) => {
res.send('todo 改')
})
/** 删除任务 */
router.delete('/', (req, res) => {
res.send('todo 删')
})
module.exports = router
在项目根路径下执行tcb一键部署之后,使用postman来测试上述接口👇
成功!