Home > Net >  POST API not working in express /node.js with mysql
POST API not working in express /node.js with mysql

Time:08-30

I am learning nodejs..i make a post api with static data with mysql database My config.js file :

const mysql = require('mysql')

const con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"",
    database:"test"

})

module.exports =con;

and my index.js file:

    const express = require('express');
const con = require('./config')

const app = express();

app.post('/', (req, res) => { 
   const data = {
      name: "xyz",
      email: "[email protected]",
      mobileno: "88888888",
      Address: "xyz place"
   }
   con.query('INSERT INTO biodata SET ?', data, (err, result, field) => {
      if (err) {
         res.send("error data not inserted")
      }
      console.log(result)
      res.send(result);
   })
})

app.listen(5000)

when i am running the code it giving me error:

GET http://localhost:5000/ 404 (Not Found)

i am doing POST api but it giving me abov error

CodePudding user response:

Try running your server and copy paste the following into the terminal:

curl --location --request POST 'http://localhost:5000/'

The command above will make a post request to your service from the terminal. If you haven't installed curl, download it here.

CodePudding user response:

Accessing a URL such as yours (http://localhost:5000/) via web browser will always use GET method. If you want to make a request with POST, you have to use other way.

For example, you can write a code that makes POST request, or you can simulate a request to your end something like Postman.

  • Related