javascript 获取 restify REST API 服务器以支持 HTTPS 和 HTTP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33666226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get restify REST API server to support both HTTPS and HTTP
提问by user781486
I am using node.js restify ver4.0.3
我正在使用 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
以下简单的代码用作支持 HTTP 的简单 REST API 服务器。API 调用示例是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
假设我想支持 HTTPS 并进行 API 调用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.
我注意到 restify 代码更改非常快,旧版本的旧代码可能不适用于最新版本。
回答by user781486
Thanks to the comment from Bas van Stein, here is a complete working example.
感谢 Bas van Stein 的评论,这里有一个完整的工作示例。
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);
});
回答by ps95
To use HTTPS, you need a key and a certificate:
要使用 HTTPS,您需要一个密钥和一个证书:
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 和 HTTPS 访问:
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 和 HTTPS 之间重定向:
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.
以上监听对路由的请求,/1
并简单地将其重定向到处理它的 HTTPS 服务器。