admin管理员组文章数量:1200756
I am using node.js restify ver4.0.3
The simple following code works as a simple REST API server that supports HTTP. An example API call is http://127.0.0.1:9898/echo/message
var restify = require('restify');
var server = restify.createServer({
name: 'myapp',
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
//http://127.0.0.1:9898/echo/sdasd
server.get('/echo/:name', function (req, res, next) {
res.send(req.params);
return next();
});
server.listen(9898, function () {
console.log('%s listening at %s', server.name, server.url);
});
Suppose I want to support HTTPS and make the API call https://127.0.0.1:9898/echo/message
How can this be done?
I noticed that restify code changes pretty fast and older code with older version may not work with the latest version.
I am using node.js restify ver4.0.3
The simple following code works as a simple REST API server that supports HTTP. An example API call is http://127.0.0.1:9898/echo/message
var restify = require('restify');
var server = restify.createServer({
name: 'myapp',
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
//http://127.0.0.1:9898/echo/sdasd
server.get('/echo/:name', function (req, res, next) {
res.send(req.params);
return next();
});
server.listen(9898, function () {
console.log('%s listening at %s', server.name, server.url);
});
Suppose I want to support HTTPS and make the API call https://127.0.0.1:9898/echo/message
How can this be done?
I noticed that restify code changes pretty fast and older code with older version may not work with the latest version.
Share Improve this question asked Nov 12, 2015 at 7:26 guagay_wkguagay_wk 28k63 gold badges198 silver badges309 bronze badges 2- 1 Did you check qugstart.com/blog/node-js/…? – Niki van Stein Commented Nov 12, 2015 at 7:35
- Thanks. Looks good. I'm trying out an example based on that link. Some problems at the moment. – guagay_wk Commented Nov 12, 2015 at 8:14
2 Answers
Reset to default 12To use HTTPS, you need a key and a certificate:
var https_options = {
key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};
var https_server = restify.createServer(https_options);
You will need to start both servers for allowing both HTTP and HTTPS access:
http_server.listen(80, function() {
console.log('%s listening at %s', http_server.name, http_server.url);
});.
https_server.listen(443, function() {
console.log('%s listening at %s', https_server.name, https_server.url);
});.
To configure routes to server, declare same routes for both servers, redirecting between HTTP and HTTPS as needed:
http_server.get('/1', function (req, res, next) {
res.redirect('https://www.foo.com/1', next);
});
https_server.get('/1', function (req, res, next) {
// Process the request
});
The above listens to requests to a route /1
and simply redirects it to the HTTPS server which processes it.
Thanks to the comment from Bas van Stein, here is a complete working example.
var restify = require('restify');
var fs = require('fs');
// Setup some https server options
//generated from http://www.selfsignedcertificate.com/
var https_options = {
key: fs.readFileSync('./HTTPS.key'), //on current folder
certificate: fs.readFileSync('./HTTPS.cert')
};
// Instantiate our two servers
var server = restify.createServer();
var https_server = restify.createServer(https_options);
// Put any routing, response, etc. logic here. This allows us to define these functions
// only once, and it will be re-used on both the HTTP and HTTPs servers
var setup_server = function(app) {
function respond(req, res, next) {
res.send('I see you ' + req.params.name);
}
// Routes
app.get('/test/:name', respond);
}
// Now, setup both servers in one step
setup_server(server);
setup_server(https_server);
// Start our servers to listen on the appropriate ports
server.listen(9848, function() {
console.log('%s listening at %s', server.name, server.url);
});
https_server.listen(443, function() {
console.log('%s listening at %s', https_server.name, https_server.url);
});
本文标签: javascriptGet restify REST API server to support both HTTPS and HTTPStack Overflow
版权声明:本文标题:javascript - Get restify REST API server to support both HTTPS and HTTP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738614009a2102799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论