简体   繁体   中英

Node JS web service based on api key

I have some web services, written in node js and express. I want to use api key based service. Suppose I currently have a web service "getRooms".

app.get('/getRooms/', function (req, res) {
    'use strict';
    N.API.getRooms(function (rooms) {
        res.send(rooms);
    },function (err) {
        res.send(err);
    });
});

I am using it by calling https://xxxxxx/getRooms . Now I want to use https://xxxxxx/APIKEY/getRooms . This APIKEY is different for different clients. So how do I modify my web services to achieve this? Please help. I'm new in Node JS

You can modify it as /APIKEY/getrooms to achieve the desired result. However it would be better it you send APIKEY in request header but obviously it depends on your case.

you can modify your code like this

app.get('/getRooms?api_key=xxxx', function (req, res) {
    console.log(req.query.api_key) // print xxxx when user request to https://xxxxxx/getRooms/APIKEY
});

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