I am newbie to Nodejs. I am creating apis using node, express and Postgres DB.
const {Pool} = require('pg');
//const connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/springmvctutorial';
var http = require("http");
var express = require('express');
const router = express.Router();
const app = express();
var bodyParser = require('body-parser');
var apiVersion = '/api/v1/';
const poo = new Pool({
database: 'springmvctutorial',
host: 'localhost',
user: 'postgres',
password : 'root',
port: 5432,
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
})
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
//create app server
var server = app.listen(3030, "localhost", function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});
router.post('/api/v1/postData', (req, res, next) => {
poo.connect((err, client, release) => {
if (err) {
return console.error('Error acquiring client', err.stack);
}
client.query('INSERT INTO items(text, complete) values(1,false)', (err, result) => {
release();
if (err) {
return console.error('Error executing query', err.stack);
}
console.log(result.rows);
});
});
});
module.exports = router;
My table and DB has been created. I checked from Postgres CLI. But when I am running my main.js file using node main.js. It starts listening to the port on the local host but when I fire,
http://127.0.0.1:3030/api/v1/postData
It gives 404 resource not found error.
My input from postman is ,
{
"text":"[email protected]",
"complete": true
}