Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 Not found error in Postgres and NodeJs api (May be duplicate)

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
}
like image 580
Custadian Avatar asked Apr 28 '26 14:04

Custadian


1 Answers

try like this, i didn't test it due to i don't have the environment in this machine

const {Pool} = require('pg');
//const connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/springmvctutorial';
var http = require("http");
var express = require('express');
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
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)
});


app.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);
    });
  });

});
like image 150
Basil Battikhi Avatar answered Apr 30 '26 03:04

Basil Battikhi