保护发布路线NodeJS [英] Protecting post routes NodeJS

查看:103
本文介绍了保护发布路线NodeJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个nodeJS应用程序。到目前为止,我已经了解到您可以使用JWT保护路线,并且已经实现了这一点。问题是,我不确定如何保护允许用户发布的路由。

I am working on a nodeJS application. So far i've learned you can protect routes with JWT and i've implemented this. The problem is, I am not sure how to protect a route where the user is allowed to post to.

让我们以注册路由为例,将使用该路由用于用户注册。我希望用户只能从我的应用程序发布到此,但。我不希望任何人都能通过邮递员连接到它并发布任何内容。您如何保护这些路线。

Let's take a register route for example, this route will be used for user registration. I want users to be able to post to this, BUT only from my application. I dont want anyone to just be able to connect to it via postman and post whatever. How do you protect these kind of routes.

预先感谢。

推荐答案

您可以使用CORS中间件来仅允许特定的客户端访问您的服务器 https://expressjs.com/en/resources/ middleware / cors.html

You can use CORS middleware to allow only specific clients to access your server https://expressjs.com/en/resources/middleware/cors.html

示例:

var express = require('express')
var cors = require('cors')
var app = express()

var corsOptions = {
  origin: 'http://example.com',
}

app.get('/products/:id', cors(corsOptions), function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for only example.com.'})
})

app.listen(80, function () {
  console.log('CORS-enabled web server listening on port 80')
})

这篇关于保护发布路线NodeJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆