1. 项目目录结构如下
    2. .
    3. |-- README.md
    4. |-- cloudbaserc.json
    5. |-- functions
    6. | `-- node-app
    7. | |-- app.js
    8. | |-- index.js
    9. | |-- package-lock.json
    10. | |-- package.json
    11. | |-- router
    12. | | |-- index.js
    13. | | `-- todo.js
    14. | `-- www.js
    15. `-- package.json
    1. // todoList\functions\node-app\index.js
    2. const serverless = require('serverless-http')
    3. const app = require('./app')
    4. const handler = serverless(app)
    5. exports.main = async (event, context) => {
    6. const res = await handler(event, context)
    7. return res
    8. }
    1. // todoList\functions\node-app\www.js
    2. const app = require('./app')
    3. app.listen(3000, () => {
    4. console.log('服务运行于http://127.0.0.1:3000');
    5. })
    1. // todoList\functions\node-app\app.js
    2. const express = require('express')
    3. const app = express()
    4. app.use(express.json())
    5. app.use(express.urlencoded({
    6. extended: false
    7. }))
    8. const indexRouter = require('./router/index')
    9. const todoRouter = require('./router/todo')
    10. app.use('/', indexRouter)
    11. app.use('/todo', todoRouter)
    12. module.exports = app
    1. // todoList\functions\node-app\router\index.js
    2. const express = require('express')
    3. const router = express.Router()
    4. router.get('/', (req, res) => {
    5. res.send('index router')
    6. })
    7. module.exports = router
    1. // todoList\functions\node-app\router\todo.js
    2. const express = require('express')
    3. const router = express.Router()
    4. /** 获取任务 */
    5. router.get('/', (req, res) => {
    6. res.send('todo router 查')
    7. })
    8. /** 添加任务 */
    9. router.post('/', (req, res) => {
    10. res.send('todo 增')
    11. })
    12. /** 修改任务 */
    13. router.put('/', (req, res) => {
    14. res.send('todo 改')
    15. })
    16. /** 删除任务 */
    17. router.delete('/', (req, res) => {
    18. res.send('todo 删')
    19. })
    20. module.exports = router

    在项目根路径下执行tcb一键部署之后,使用postman来测试上述接口👇
    image.png
    image.png
    成功!