Node.js 在azure web app中解析表单数据
我在azure上测试了一个nodejs web应用程序,它使用azure cosmos db插入和检索数据。该web应用程序基于express框架、用于数据库交互的mongoose、用于模板的pug和用于解析web表单发送的数据的body解析器 我遇到的问题是,我无法访问web表单发送的数据,基于此,我希望在azure cosmos db中插入一个新文档。基本上,当我提交web表单时,应用程序应该在body parser模块的帮助下访问表单数据,在数据库中创建一个新文档,并刷新包含新创建文档信息的视图 下面是这个小应用程序的代码Node.js 在azure web app中解析表单数据,node.js,azure,azure-web-app-service,body-parser,Node.js,Azure,Azure Web App Service,Body Parser,我在azure上测试了一个nodejs web应用程序,它使用azure cosmos db插入和检索数据。该web应用程序基于express框架、用于数据库交互的mongoose、用于模板的pug和用于解析web表单发送的数据的body解析器 我遇到的问题是,我无法访问web表单发送的数据,基于此,我希望在azure cosmos db中插入一个新文档。基本上,当我提交web表单时,应用程序应该在body parser模块的帮助下访问表单数据,在数据库中创建一个新文档,并刷新包含新创建文档信息
const express = require('express');
const http = require('http');
const mongoose= require('mongoose');
const bodyParser = require('body-parser');
const app = express();
var port = process.env.PORT || 5050; //normalizePort(process.env.PORT || '5050');
app.set('port', port);
app.set('views', 'views');
app.set('view engine', 'pug');
mongoose.connect(`mongodb://[user].documents.azure.com:10255/testdb?ssl=true`, {
auth: {
user: `[username]`,
password: `[password]`
}
})
.then(() => console.log('connection successful'))
.catch((err) => console.error(err));
let citySchema = new mongoose.Schema ({
id: {type: String, required: true},
name: {type: String, required: true},
country: {type: String, required: true}
});
const Cities = mongoose.model('Cities', citySchema);
let getData = function(request, response) {
Cities.find({}, function(err, data) {
if (err) { return "There was an error";}
console.log(JSON.stringify(data));
response.render('index', {
panelTitle: "Azure CosmosDB Test",
panelBody:"Testing how to access Azure ComsosDB",
cities: data
});
});
};
let urlencoded = bodyParser.urlencoded({extended: false});
app.get('/', function(request, response) {
getData(request, response);
});
app.get('/error', function(request, response) {
response.render('error');
})
app.post('/', urlencoded, function(request, response){
console.log(request.body);
let newCity = new Cities({id: request.body.id, name: request.body.name,
country: request.body.country});
console.log(newCity);
newCity.save(function(err, savedCity){
console.log(JSON.stringify(savedCity));
});
response.redirect('back');
});
app.use(express.static(__dirname + '/public'));
const server = http.createServer(app);
server.listen(port);
在localhost上,代码工作正常,它在数据库中插入新文档,然后在刷新视图中,新城市显示在基于pug文件生成的列表上
然而在Azure上什么也没发生。视图已刷新,但新文档未创建,当然,视图不包含web表单发送的信息。数据库连接没有问题,因为应用程序能够提取信息以生成城市列表。此外,如果在post路线中,当我创建一个新城市并尝试保存它时,我使用如下直接值:
let newCity = new Cities({id: 'MUN", name: "Munich",
country: "Germany"});
文档被创建并保存在azure cosmos数据库中,视图包含新数据
在我看来,req.body是空的,表单数据没有被解析,尽管我在这方面使用了body解析器模块。在浏览器中,开发者工具-网络选项卡-显示发送到服务器的表单数据,状态代码为302
如果你有任何想法,为什么这是不工作的azure请让我知道
谢谢 确保已使用语句app.usebodyParser.json; 使您的json数据被bodyParser识别 没有它,我会得到和你一样的空结果{}
BodyParser无法识别传入的正文格式,除非我们指定它。如果我的建议有效,您可以接受它。如果没有,请随时提出进一步的问题。我将在本周末进行测试,我会让你知道。它现在正在工作!非常感谢。仔细阅读文档中的body解析示例,我的印象是bodyParser.json和bodyParser.urlencoded应该单独使用,而不是一起使用。你知道为什么localhost上不使用app.usebodyParser.json就可以工作吗?谢谢@Luciantase不知道……在我这方面,如果错过了这句话,它在localhost上也不起作用。也许您可以检查节点版本。我的节点是v8.11.1。