0

I'm working on a REST API Post request to insert new data into a SQL database, but it doesn't work. Here is my code:

var express = require('express'); // Web Framework
var app = express();
var sql = require('mssql'); // MS Sql Server client
const { request } = require('http');

// Connection string parameters.
var sqlConfig = {
    user: 'username',
    password: 'password',
    server: 'serveraddress',
    database: 'databasename'
}

// Start server and listen on http://localhost:8081/
var server = app.listen(8081, function () {
    var host = server.address().address
    var port = server.address().port

    console.log("app listening at http://%s:%s", host, port)
});

  //POST API
 app.post("/tag/insert/:tagId/", function(req , res){
    sql.connect(sqlConfig, function() {
        var request = new sql.Request();
        var stringRequest = "INSERT INTO Tag (tagId, tagStatus) VALUES ("+ req.params.tagId+ ", 1)";
        request.query(stringRequest, function(err, recordset) {
            if(err) console.log(err);
            res.end(JSON.stringify(recordset)); // Result in JSON format
        });
    });
})

If i try to insert a new tag via browser like "http://localhost:8081/tag/insert/23232" it always says "Cannot GET /tag/insert/23232". What its mean? How solve it?

1
  • 1
    You have "/tag/insert/:tagId/" a slash at the end, maybe that's your issue. Commented Jun 17, 2020 at 13:02

1 Answer 1

1

It means that you are using a GET request while you created a POST endpoint. I'm not sure if this will work but you can try to change app.post to app.get so you actually have an GET endpoint.

I suggest you read up on HTTP request methods.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.