Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Get/index.html_Javascript_Node.js_Express_Webserver - Fatal编程技术网

Javascript Get/index.html

Javascript Get/index.html,javascript,node.js,express,webserver,Javascript,Node.js,Express,Webserver,因此,我使用node.js为express Web服务器提供服务 我能抓住: 但我不能像这样接球: express.get('/information.html',function(req,res) { // not working }) 如何获得一个捕获来获取信息和information.html-可能使用regex或类似的东西。。我不知道。我想您可以使用内置的res.sendFile功能,并将其与req.baseUrl结合起来来实现这一点。 类似的方法应该会奏效: express.us

因此,我使用node.js为express Web服务器提供服务

我能抓住:

但我不能像这样接球:

express.get('/information.html',function(req,res) {
  // not working
})

如何获得一个捕获来获取信息和information.html-可能使用regex或类似的东西。。我不知道。

我想您可以使用内置的res.sendFile功能,并将其与req.baseUrl结合起来来实现这一点。 类似的方法应该会奏效:

express.use('/information', function(req, res) {
  res.sendFile(path.join(__dirname + req.baseUrl + '.html'));
});

您必须更加具体,是否确保使用以下行要求并执行express:

var express = require("express") //Require
var app = express() 
通过这种方式,您可以确保express也在应用程序中工作。如果您根据路由指定服务器的工作方式,则无法放置html。这里有一个链接可能会帮助你

app.Get是一种根据浏览器上的路径呈现特定文档的方法

我希望有帮助

看看:

调用中间件功能的路径;可以是以下任何一种:

表示路径的字符串。 路径模式。 匹配路径的正则表达式模式。 上述任意组合的数组。 因此,您可以为此提供正则表达式:

app.get(/\/information(?:\.html)?$/, (req, res) => res.send('Hello World!'))

正如我所提到的,如果有一个文件information.html,它将不会被捕获。不起作用。@jonas00,实际上这取决于你到底需要什么样的回应,你能描述一下你想要做什么以及你需要什么样的数据吗?@jonas00很抱歉,这是对你需要做什么的误解:昆汀的答案非常完美。
app.get(/\/information(?:\.html)?$/, (req, res) => res.send('Hello World!'))