简体   繁体   English

如何在 Vue 中使用 Express

[英]How to use Express with Vue

First time using Node js, Express and Vue.第一次使用 Node js、Express 和 Vue。

I have a pomodoro timer website made with vue (not vue-app/vue-cli) which I'm wanting to upload to Heroku.我有一个用 vue(不是 vue-app/vue-cli)制作的番茄钟网站,我想上传到 Heroku。 The website works perfectly locally with Webstorm IDE, but can't manage to make it work with Express.该网站在本地与 Webstorm IDE 完美配合,但无法使其与 Express 配合使用。

Github link of project: https://github.com/marcelmiro/Pomodoro项目Github链接: https ://github.com/marcelmiro/Pomodoro

This is how my files are organized:这是我的文件的组织方式:

+-- node_modules
  -- vue & express modules
+-- scripts
  -- main.css
  -- main.js (where my vue code is)
  -- bundle.js (me testing how to use a webpack. its not being used atm)
+-- assets
  -- all images
+-- index.html
+-- package.json
+-- Procfile
+-- server.js

server.js: server.js:

const express = require('express');
const app = express();
const port = process.env.PORT || 8080;

app.use("/scripts", express.static("scripts"));

app.get('/', (req, res) => res.sendFile(__dirname + "/index.html"));

app.listen(port, () => {
    console.log('Server connected at:',port);
});

So I can manage to load css (although not all css code), but not main.js (where my vue code is).所以我可以设法加载 css(虽然不是所有的 css 代码),但不能加载 main.js(我的 vue 代码所在的位置)。 Images are also not loaded.图像也未加载。

How can I solve this?我该如何解决这个问题?

As index.html is in your root folder, you just need to tell express to look there;由于index.html在你的根文件夹中,你只需要告诉 express 看那里;

const express = require('express');
const app = express();
const port = process.env.PORT || 8080;

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

app.listen(port, () => {
    console.log('Server connected at:',port);
});

In practice you would want to separate your source code from your production code, but that's a little out of scope for this question.在实践中,您可能希望将源代码与生产代码分开,但这有点超出了这个问题的范围。

If you're using Vue to create a single page application, you might be using history mode .如果您使用 Vue 创建单页应用程序,您可能正在使用 历史模式 In that case, here is a full solution with ssl and http redirects to https:在这种情况下,这是一个完整的解决方案,将 ssl 和 http 重定向到 https:

/* server.js */
const fs = require('fs')
const https = require('https')
const http = require('http')
var express = require('express')
const history = require('connect-history-api-fallback');
const app = express()
const redirectHttp = express()

/*
 * Redirect http to https
 */
redirectHttp.get("*", (req,res,next) => {
        res.redirect("https://" + req.headers.host + req.url)
})

// USE HMTL5 History API, allows Vue to handle the routing
const staticFileMiddleware = express.static("./dist");
app.use(staticFileMiddleware);
app.use(history({}));
app.use(staticFileMiddleware);

http.createServer(redirectHttp).listen(80,()=>{console.log('Listening for http requests...')})

https
  .createServer(
    {
      key: fs.readFileSync('path/to/key'),
      cert: fs.readFileSync('path/to/cert'),
      ca: fs.readFileSync('path/to/cert'),
    },
    app
  )
  .listen(443, () => {
    console.log('Listening for https requests...')
  })

This assumes your dist directory is in the same folder as server.js这假设您的 dist 目录与 server.js 位于同一文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM