How to deploy a Vue.js application on Node.js server

后端 未结 1 567
太阳男子
太阳男子 2020-12-10 18:47

I have a dist folder containing CSS, fonts, JS folder and an index.html file minimized for Vue.js, ready to deploy and use. I want to use Node.js to run this ap

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 19:26

    Since Vue is only a frontend library, the easiest way to host it and do things like serve up assets is to create a simple Express friendly script that you can use to start a mini-web server. Read up quickly on Express if you haven’t already. After that, add express:

    npm install express --save
    

    Now add a server.js file to your project’s root directory :

    // server.js
    var express = require('express');
    var path = require('path');
    var serveStatic = require('serve-static');
    app = express();
    app.use(serveStatic(__dirname + "/dist"));
    var port = process.env.PORT || 5000;
    var hostname = '127.0.0.1';
    
    app.listen(port, hostname, () => {
       console.log(`Server running at http://${hostname}:${port}/`);
     });
    

    after that you could run :

     node server
    

    and your project will be served at the given host and port

    Assuming that you have already the dist directory, if you don't have it run :

    npm run build
    

    in order to generate it

    0 讨论(0)
提交回复
热议问题