在生产中快速提供静态和api服务

时间:2018-07-05 22:27:40

标签: node.js express webpack vue.js

我已将Express / Vue应用程序部署到生产环境中。 我发现从express提供静态文件和API有点困难。

在expressfolder / app.js内部

const app = express()
const path = require('path')
const serveStatic = require('serve-static')

...

// index.html and static/css, static/js - bundle made with npm run build
const DIR_DIST = path.join(__dirname, '../../path/to/dist')
app.use(serveStatic(DIR_DIST))

...

app.get('/tests', (req, res) => {
  res.send({msg: 'Hello there!'})
})
  • 当我访问myapp.com时,会看到所需的index.html。
  • 如果我直接在浏览器myapp.com/tests中键入,我会看到来自express的原始消息“ hello there”。
  • 如果我通过index.html中的链接调用相同的路由,则会在chrome中收到此错误
  

(失败)net :: ERR_CONNECTION_REFUSED

它在我的本地计算机上正常工作,所以我确定它是某种杂乱的配置,我没有正确设置。

此外,我不想直接访问/tests:vue-router应该重写它,但这是一个较小的问题。

可能以前有人问过,但是已经有一段时间了,我还没有找到解决方案。 谢谢。

1 个答案:

答案 0 :(得分:1)

错误<div id="main"> <div id="header"><b>Phone Book</b></div> <div id="addnew"> <div id="entry"> <span>Name:</span><span><input class="inputstyle name" type="text" placeholder="Insert name..."/></span><span>PhoneNo:</span><span><input class="inputstyle phone" type="number" placeholder="Insert phone number..."/></span><br> <button id="add" type="button" value="Add new entry" onclick="addEntry();">Add Contact</button> </div> </div> <div id="newEntry"> <table id="myTable"> <thead> <tbody> </tbody> <td></td> <td></td> <td></td> <td></td> </thead> </table> </div> </div>是一个基本的网络错误,发生在服务器代码实际运行之前,因此与您的特定服务器代码无关。

这可能是因为您的网页中的URL错误。