Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Node.js 节点/Express根据请求发送静态文件_Node.js_Express - Fatal编程技术网

Node.js 节点/Express根据请求发送静态文件

Node.js 节点/Express根据请求发送静态文件,node.js,express,Node.js,Express,我对node/express非常陌生,我正试图了解发送静态文件是如何工作的。我设法提供了我的索引文件,但我不能提供其他文件作为GET请求的响应 app.use(express.static(path.join(__dirname, '/client/build'))) app.get('/', (req, res) => { res.sendFile(path.resolve(__dirname, '.', 'client/build/', 'index.html')) res.

我对node/express非常陌生,我正试图了解发送静态文件是如何工作的。我设法提供了我的索引文件,但我不能提供其他文件作为GET请求的响应

app.use(express.static(path.join(__dirname, '/client/build')))

app.get('/', (req, res) => {
  res.sendFile(path.resolve(__dirname, '.', 'client/build/', 'index.html'))
  res.end()
})

app.get('/portfolio', (req, res) => {
  const person = req.query.name
  var filePath = __dirname + '/client/build/' + person + '/' + 'index.html'
  res.sendFile(filePath)
  res.end()
})
我发现了类似的问题,但似乎没有任何效果

我发送的请求是:

fetch(`portfolio?name=${who}`)

你的代码有一些问题。其中一个问题是在文件发送完成之前就结束了请求,另一个问题是没有正确使用
res.sendFile
方法

试着这样做:

app.get('/', (req, res) => {
  const fileDirectory = path.resolve(__dirname, '.', 'client/build/');

  res.sendFile('index.html', {root: fileDirectory}, (err) => {
    res.end();

    if (err) throw(err);
  });
})

app.get('/portfolio', (req, res) => {
  const person = req.query.name
  const fileDirectory = __dirname + '/client/build/' + person + '/';

  res.sendFile('index.html', {root: fileDirectory}, (err) => {
    res.end();

    if (err) throw(err);
  });
})
<a href='/person1'></a>
<a href='/person2'></a>

我不建议您在遇到错误时抛出错误,但它至少可以让您了解如何使其正常工作。

我最终通过在服务器上使用此选项解决了此问题:

app.use( express.static(path.join(__dirname, 'client/build/person1')))
app.use( express.static(path.join(__dirname, 'client/build/person2')))
在视图中这样称呼它:

app.get('/', (req, res) => {
  const fileDirectory = path.resolve(__dirname, '.', 'client/build/');

  res.sendFile('index.html', {root: fileDirectory}, (err) => {
    res.end();

    if (err) throw(err);
  });
})

app.get('/portfolio', (req, res) => {
  const person = req.query.name
  const fileDirectory = __dirname + '/client/build/' + person + '/';

  res.sendFile('index.html', {root: fileDirectory}, (err) => {
    res.end();

    if (err) throw(err);
  });
})
<a href='/person1'></a>
<a href='/person2'></a>

看起来,express.static自己解析路径,所以您不需要自己处理请求来服务静态文件


如果这不是一个好的解决方案,请询问

您向服务器发送了什么请求?什么是
人员
,您向服务器发送了什么请求@alexmacI编辑了这个问题,请求是fetch(
portfolio?name=${who}
),其中“who”是我尝试过的投资组合所有者的名称(字符串),但它不起作用。正如有人指出的,第一个请求处理程序(“/”)不是必需的,因为express.static映射路由。但是第二个仍然不起作用,如果我放一个console.log,它确实会记录一些东西,因此请求正在被处理,但它不会做任何事情,也不会抛出错误