启动并运行节点API

时间:2017-11-25 21:50:01

标签: node.js postgresql rest api

我正在尝试关注this tutorial使用node,Express和postgresql来构建和API。我的所有代码都可以在Github上找到。

在我的服务器/ routes / index.js中是以下(编号是行号)。

1: app.get('/api/todos', todosController.list);
2: // app.post('/api/todos/:todoId/items', todoItemsController.create);
3: app.post('/api/todos', todosController.create);
4: app.get('/api/todos/:todoId', todosController.retrieve);
5: app.put('/api/todos/:todoId', todosController.update);
6: app.delete('/api/todos/:todoId', todosController.destroy);

我遇到两个问题。

第一个问题是第4和第5行不起作用(所有其他行都是第2行)。当我使用邮递员/浏览器测试它时,我收到此错误

ReferenceError: TodoItem is not defined
    at retrieve (/Users/X-lem/Development/TestAPI/server/controllers/todos.js:25:20)
    at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
    at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:281:22
    at param (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:354:14)
    at param (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:410:3)
    at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/Users/X-lem/Development/TestAPI/node_modules/body-parser/lib/types/urlencoded.js:91:7)
    at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:317:13)
    at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
    at jsonParser (/Users/X-lem/Development/TestAPI/node_modules/body-parser/lib/types/json.js:109:7)
    at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:317:13)
    at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)

其次,当我取消注释第2行时,控制台崩溃,我收到此错误。

ReferenceError: todoItemsController is not defined
    at module.exports (/Users/X-lem/Development/TestAPI/server/routes/index.js:9:40)
    at Object.<anonymous> (/Users/X-lem/Development/TestAPI/app.js:16:27)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load (module.js:486:3)
    at Module.require (module.js:556:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/X-lem/Development/TestAPI/bin/www:3:13)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load (module.js:486:3)
    at Function.Module.runMain (module.js:653:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...

我是建立API的新手,我正在努力教会自己如何做到这一点。我尝试了许多不同的故障排除方法但无济于事。感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

希望我能帮助一下。你的问题#2是因为你做错了。请查看您的routes/index.js文件。您将控制器导入变量todosController,但在第9行,您使用的是未定义的变量todoItemsController。所以就 app.post('/api/todos/:todoId/items', todosController.create);可以使用。