`const httpContext = require('request-context'); const app = express(); app.use(httpContext.middleware( '请求'));
httpContext.set('xxxxx','jnsdkj'); ` 它抛出一个错误'找不到设置属性的活动上下文' 它在当地工作但不在天蓝色工作 它曾经奏效过。
答案 0 :(得分:0)
我检查了包request-context并创建了一个示例azure web app来检查此问题。
<强> app.js 强>
const express = require('express')
const app = express()
const cookieParser = require('cookie-parser')
const httpContext = require('request-context')
app.use(httpContext.middleware('request'))
app.use(cookieParser())
app.use(function (req, res, next) {
var cookie = req.cookies.uname;
console.log("usercookie:"+cookie);
if(cookie!=undefined){
// set the user who made this request on the context
httpContext.set('request:user', {username:cookie,loggedTime:req.cookies.loggedTime});
}
next();
});
app.get('/login', (req, res) => {
var user=req.query.name;
if(user==undefined)
res.send('you need to pass the username.');
else{
//set a cookie
res.cookie('uname',user, { maxAge: 900000, httpOnly: true });
res.cookie('loggedTime',new Date(), { maxAge: 900000, httpOnly: true });
console.log('cookie created successfully');
res.send('login successed.');
}
});
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/me',(req,res)=>{
res.send('Hello World!'+httpContext.get('request:user.username')+",loggedTime:"+httpContext.get('request:user.loggedTime'));
});
app.listen(process.env.PORT || 3000, () => console.log('Example app listening on port 3000!'))
<强>的package.json 强>
"dependencies": {
"cookie-parser": "^1.4.3",
"express": "^4.16.3",
"request-context": "^2.0.0"
}
<强> TEST:强>
https://brucenode.azurewebsites.net/login?name=bruce
https://brucenode.azurewebsites.net/me
httpContext.set('xxxxx','jnsdkj'); `它抛出错误'找不到设置属性的活动上下文'
我认为您可能需要检查代码并验证调用httpContext.set
的位置。对于Azure Web应用程序,您还可以Enable diagnostics logging for web apps in Azure App Service解决此问题。
答案 1 :(得分:0)
本地我在azure中使用节点版本8.7.0,允许最多10.0 如果我们在包中提及指定版本,则azure使用最新版本 为了避免这种混乱
在package.json中提及您正在开发的版本
"engines": {
"node": "8.7.0" // mentioning >= causes to use latest version
},