我正在try 用Node和Postgres建立一个RESTful API.我遇到了一个问题,每当我试图运行服务器(使用npm start)在本地对其进行测试时,我都会得到以下输出:

[nodemon]1.14.10[nodemon]要随时重新启动,请输入rs[nodemon]观看:.[nodemon]开始node index.js server.js

在网上搜索了很长一段时间后,我找不到太多关于"干净退出-重新启动前等待更改"确切含义的资源,尤其是在这种情况下.

这是我的疑问.js文件:

  1 var promise = require('bluebird');
  2 
  3 var options = {
  4   // Initialization Options
  5   promiseLib: promise
  6 };
  7 
  8 // created an instance of pg-promise, override default pgp lib w bluebird
  9 var pgp = require('pg-promise')(options);
 10 var connectionString = 'postgres://localhost:3000/actions';
 11 var db = pgp(connectionString);
 12 
 13 // add query functions
 14 
 15 module.exports = {
 16   getAllActions: getAllActions,
 17 //  getSingleAction: getSingleAction,
 18 //  createAction: createAction,
 19 //  updateAction: updateAction,
 20 //  removeAction: removeAction
 21 };
 22 
 23 function getAllActions(req, res, next) {
 24   db.any('select * from acts')
 25     .then(function (data) {
 26       res.status(200)
 27         .json({
 28           status: 'success',
 29           data: data,
 30           message: 'Retrieved ALL actions'
 31         });
 32     })
 33     .catch(function (err) {
 34       return next(err);
 35     });
 36 }

这是我的索引.js文件:

  3 var express = require('express');
  4 var app = express();
  5 var router = express.Router();
  6 var db = require('./queries');
  7 
  8 // structure: expressInstance.httpRequestMethod(PATH, HANDLER)
  9 app.get('/api/actions', db.getAllActions);
 10 //app.get('/api/actions/:id', db.getSingleAction);
 11 //app.post('/api/actions', db.createAction);
 12 //app.put('/api/actions/:id', db.updateAction);
 13 //app.delete('/api/actions/:id', db.removeAction);
 14 
 15 module.exports = router;

你有什么 idea 吗?提前谢谢.

推荐答案

你的代码有一些问题.你从未让你的应用程序运行.当你准备好创建路由时,你应该用以下方法启动服务器:

app.listen(<port on which the server should run here>);

同样,你在同一个文件中有一个Express应用程序和一个路由.路由只能作为一个子模块使用(当你想把你的应用程序分成多个文件时很方便).现在你什么都没做.如果移除路由和导出,那么它应该可以正常工作.

Postgresql相关问答推荐

环境变量在Bash应用程序中没有出现

在Postgres中,如何删除包含N个自然数的表,并替换为生成子查询?

在rust中,有什么方法可以从sqlx::query_with中返回rows_infected以及行本身?

如何返回old_ids和重复行的映射';来自PostgreSQL函数的s new_id

将具有自定义类型的表从一种模式复制到另一种模式

如何获得一起满足数量要求的物品? (WHILE 循环还是 CTE?)postgresql

PostgreSQL 不删除旧的 WAL 档案

无法将 json 范围读取为 pgtype.Int4range

Select 与输入数组完全相交的所有行?

如何将特定值排序为最后,其余记录按 id DESC 排序?

SQLAlchemy 中使用什么类型存储字节字符串?

返回 NULL 的空数组的 array_length()

如何为查询执行设置语句超时

从time without time zone和时区名称中获取time with time zone

Postgres 外键on update和on delete选项如何工作?

如何将主键添加到视图?

python pip install psycopg2 安装错误

在 Postgres 中显示关系、序列和函数的默认访问权限

使用 PostgreSQL 的 Linq To Sql

PostgreSQL - GROUP BY 子句或用于聚合函数