如何在Nodejs中保护RestFul Api?

现在我正在使用https://github.com/baugarten/node-restful来帮助我在API中工作,问题是?

我正在使用Express框架,有没有办法保护来自其他网站的“GET”请求。

我使用快递的CSRF,但只能通过POST,PUT,DELETE方法使用FOrbidden 403的消息,因为在控制台中使用curl进行交换,但是如果向Get方法进行curl,curl localhost:3000 / postme数组与所有职位。

app.use(express.csrf()); app.use(function(req, res, next){ res.locals.token = req.session._csrf; next(); }); app.use(app.router); 

你有什么build议吗? 还有其他模块更好地工作一个Api? 如何保护nodejs中的Api? 什么是最好的实践,一个haver学习?

感谢您的帮助。

尝试一下这样做的Express中间件。 例如:

 var express = require('express'); var app = express(); // simple middle ware to protect your URI app.use(function(req, res, next){ if (req.method == 'GET') { if (!req.locale.token) { res.send(403); } // custom to fit your policy else if (req.protocol !== "https") {res.redirect("your-secure-url");} else { next(); } } });