【问题标题】:Fail to deploy a website with dummy backend无法部署具有虚拟后端的网站
【发布时间】:2022-01-19 20:00:24
【问题描述】:

我创建了一个仅包含 HTML 文件的项目,我想在 Heroku 上部署它。所以我创建了一个虚拟后端来部署在 Heroku 上,但是当我在 localhost 上运行它时,它会显示一个错误 no such file or directory

错误图片:

var express = require('express');
var app = express();
var path = require('path');

app.use(express.static(path.join(__dirname)));

app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + 'project.html'));
});

// add other routes below
app.get('/Vaccine2', function(req, res) {
  res.sendFile(path.join(__dirname + 'Vaccine2.html'));
});

app.get('/hospital', function(req, res) {
  res.sendFile(path.join(__dirname + 'hospital.html'));
});

app.listen(process.env.PORT || 3000);

【问题讨论】:

  • 将代码分享为文本,而不是截图

标签: html node.js heroku frontend backend


【解决方案1】:

在文件名中添加反斜杠\

app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '\project.html'));
});

// add other routes below
app.get('/Vaccine2', function(req, res) {
  res.sendFile(path.join(__dirname + '\Vaccine2.html'));
});

app.get('/hospital', function(req, res) {
  res.sendFile(path.join(__dirname + '\hospital.html'));
});

【讨论】:

    【解决方案2】:

    先在 Git 中删除一个分支

    rm -rf .git
    

    然后在项目上创建一个src目录

    并更改您的代码

    这个:

    app.use('/src', express.static(path.join(__dirname)));
    

    到这里:

    app.use('/src', express.static(path.join(__dirname + '/src')));
    

    转到您的 package.json 文件并添加您的 node.js 版本,例如:

    "engines": {
      "node": "14.x"
     }
    

    然后尝试再次部署您的网站

    git add .
    git commit -m "Added a Procfile."
    heroku login
    Enter your Heroku credentials.
    ...
    heroku create
    Creating arcane-lowlands-8408... done, stack is cedar
    http://arcane-lowlands-8408.herokuapp.com/ | git@heroku.com:arcane- 
    lowlands-8408.git
    Git remote heroku added
    git push heroku main
    ...
    -----> Node.js app detected
    ...
    -----> Launching... done
       http://arcane-lowlands-8408.herokuapp.com deployed to Heroku
    

    您可以查看一些推荐:

    Heroku hosting

    首先要确保在 heroku 中正确键入所有 cmets。 如果您能解决,请随时发回消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 2018-10-13
      • 2021-10-28
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多