新表达...
req.user
当我处理文件中的请求时,我可以访问app.js
。但是,即使我routes/users.js
在. 正如我所提到的,我是新来表达的,所以我可能配置不正确。非常感谢任何提示!如果不清楚或者您需要更多信息,请告诉我。谢谢!req.user
app.js
(app.use('/users', authenticate);
应用程序.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var dotenv = require('dotenv');
var jwt = require('express-jwt');
var cors = require('cors');
var http = require('http');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
var router = express.Router();
dotenv.load();
var authenticate = jwt({
secret: new Buffer(process.env.AUTH0_CLIENT_SECRET, 'base64'),
audience: process.env.AUTH0_CLIENT_ID
});
app.use(cors());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', routes);
app.use('/users', users);
app.use('/users', authenticate);
app.use('/secured', authenticate);
app.get('/ping', function(req, res) {
res.send("All good. You don't need to be authenticated to call this");
});
app.get('/secured/ping', function(req, res) {
// req.user works here!
res.status(200).send("All good. You only get this message if you're authenticated");
});
var port = process.env.PORT || 3001;
http.createServer(app).listen(port, function (err) {
console.log('listening in http://localhost:' + port);
});
module.exports = app;
路线/users.js
var models = require('../models');
var express = require('express');
var router = express.Router();
router.post('/', function(req, res) {
// req.user = undefined
console.log("user: " + req.user.sub)
models.User.findOrCreate({
where: {tokenId: req.user.sub},
defaults: {
name: req.body.name,
tokenId: req.user.sub,
points: 0
}
}).then(function(user) {
res.json(JSON.stringify(user));
});
});
module.exports = router;