[英]app.get('') is not working in node js with query parameters
Iam尝试为我的会话应用程序提供一条新的路由,该路由应接受随路由一起传递的参数,并且可以在客户端使用。但是我无法弄清楚为什么基本的.get()无法正常工作,而Iam无法呈现html。
'use strict';
var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests
var Conversation = require('watson-developer-cloud/conversation/v1'); // watson sdk
var bodyParser = require('body-parser');
var app = express();
app.use(express.static('./public')); // load UI from public folder
app.use(bodyParser.json());
app.get('/:id',function(req,res){
var userid = req.params.id;
var pid = req.query.pid;
res.sendFile(__dirname,'/public/index.html');
});
module.exports = app;
在我的localhost:3000索引文件上正在加载,但是对于localhost:3000/3405?pid = CBM之类的文件却没有加载。
然后我在客户端有一个js文件,它需要这两个值id和pid。目前我只是硬编码。但是如何将这些值用于客户端js文件。有人可以帮我怎么做.. 。 谢谢
已更新:添加我的客户端js文件
var Api = (function() {
var messageEndpoint = '/api/message';
var emp = {
"pid": "CBM",
"id": "3405",};
return {
sendRequest: sendRequest,
modifytext: function(intent, text) {
if (intent == "Hello") {
console.log(text, "Inside intent");
for (var key in emp) {
var tempKey = '{{' + key + '}}';
var tempValue = emp[key];
text = replace(text, tempKey, tempValue);
console.log("came back");
}
}
return text;
console.log(text,"Final text");
}
};
function replace(text, originalString, replaceText) {
console.log("Reached replace functions", text, originalString, replaceText);
if (replaceText)
text = text.replace(originalString, replaceText);
else
text = text.replace(originalString, "");
return text
}
}());
这是不正确的:
res.sendFile(__dirname,'/public/index.html');
应该是这样的:
res.sendFile(__dirname + '/public/index.html');
或者(更健壮):
const path = require('path');
...
res.sendFile(path.join(__dirname, 'public/index.html'));
附带说明:显然,如果将目录名传递给res.sendFile()
,它将发送回404响应。 不确定背后的理由是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.