Creating a simple webservice in nodejs

2019-04-02 08:19发布

问题:

I need to create a simple rest based webservice in node.js I already know only how to consume the already exposed ones.I used http module for that.Any suggestion on how to create a webservice will be much helpful

回答1:

You can use Expressjs, it's the most common web framework to make REST Api. Check this stackoverflow question which provides a lot of good resources. Have a look at this tutorial too.

If you need something more evolved I recommend you CompoundJS which will help you to give a structure to your code.



回答2:

Hey heres something that i tried its really simple.Below is the code

var express = require("express");
var app = express();
var data="<Bank><rows>10</rows></Bank>"
console.log(data);
app.get('/Dynamic', function(req, response) {
response.contentType('application/xml');
console.log('listening');
response.send(data);
});
console.log('server will be listening on: 1360' );
app.listen(1360);

Hope this helps .



回答3:

we can use this code

var express = require('express');

var app = express();

app.post('/login', function(req, res) {

    res.send('Got the data');
});
app.listen(3002);
console.log('Listening on port 3002...');