简体   繁体   中英

Sending parameters to Rest API for SQL Server

I have created a SQL server with a table that has data in on a server I have local access to. I have also created an "API" on that same server to get the information from the SQL server so it can be read by my angular application.

At the moment, I can read the rows in the SQL server and have them displayed in my angular application, but, I want to be able to update the SQL table from my angular app (through the API).

This is my API ( sqlserver.js ):

var express = require('express');
var app = express();
var cors = require('cors');

app.use(cors())

app.get('/', function (req, res) {

    var sql = require("msnodesqlv8");

    // config for your database
    var config = "server=servername;Database=dataBaseName;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}"
    const query = "SELECT * FROM tableName";
    // connect to your database

    sql.query(config, query, (err, rows) => {
        res.send(rows)
     });
});


var server = app.listen(3097, function () {
    console.log('Server is running..');
});

I want to be able to use the query

const query = "SELECT * from tableName WHERE ID LIKE <inputFromAngular>"

But I am not sure how to get the parameter from angular into the sqlserver.js . (If I can do this then it will lead to updating the SQL Table using SET

In my angular app this is how I am calling the sqlserver.js to display the SQL table:

import { HttpClient } from '@angular/common/http';

constructor(
    private httpService: HttpClient
    ) { }


this.httpService.get("http://servername:3097").subscribe(response => {
        console.log(response)
        this.sqlData = response;
      })

I have tried using this.httpService.post() but I wasn't sure how to get the parameters in the API?

You will need to change you get method to send params from ui side and get params on backend side so add a server side method like

app.get('/:id', function (req, res) {

    var sql = require("msnodesqlv8");
 var id = req.params.id,
    // config for your database
    var config = "server=servername;Database=dataBaseName;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}"
    const query = "SELECT * FROM tableName WHERE ID LIKE "+id;
    // connect to your database

    sql.query(config, query, (err, rows) => {
        res.send(rows)
     });
});

then on angular side

this.httpService.get("http://servername:3097/"+yourid).subscribe(response => {
        console.log(response)
        this.sqlData = response;
      })

Also I would suggest you to create a root like /yourapiname/:id instead of /:id and url on angular side should be " http://servername:3097/yourapiname "+yourid because your current root can result to confliction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM