我在这里创建了一个API休息服务器是我的路线:
const express = require('express');
const router = express.Router();
const pg = require('pg');
const path = require('path');
const connectionString = process.env.DATABASE_URL | 'postgres://user:password@host:port/database';
router.put('/', (req, res, next) => {
const results = [];
// Grab data from http request
const data = {cpu: req.body.cpu, ram: req.body.ram};
// Get a Postgres client from the connection pool
pg.connect(connectionString, (err, client, done) => {
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({success: false, data: err});
}
// SQL Query > Update Data
client.query('UPDATE cpuram SET cpu=($1), ram=($2)',
[data.cpu, data.ram]);
// SQL Query > Select Data
const query = client.query("SELECT * FROM cpuram ORDER BY 1 ASC");
// Stream results back one row at a time
query.on('row', (row) => {
results.push(row);
});
// After all data is returned, close connection and return results
query.on('end', function() {
done();
return res.json(results);
});
});
});
/* GET home page. */
//router.get('/', function(req, res, next) {
// res.render('index', { title: 'Express' });
//});
module.exports = router;
当我想发送一个用于使用此命令更新我的数据库psql的查询时:
curl -X PUT --data“cpu = 50& ram = 10”网址
他让我想起了这个错误:
{ “成功”:假, “数据”:{ “代码”: “ECONNREFUSED”, “错误号”: “ECONNREFUSED”, “系统调用”: “连接”, “地址”: “127.0.0.1”,”端口“:5432}}