简体   繁体   中英

vue files without NodeJS?

I want to host my app outside of node JS, but I want to use .vue files and possible npm as build system (if it's needed). Is it's possible to do?

I do not need any backward compatibility and if it work on latest Chrome dev it's ok for me.

Is there any examples how it can be done?

I tried to build some webpack template, but it's work only inside NodeJS. On other server I am getting 404 when I am accessing to URLs that placed in .vue files. It's seems that they can't be handled by the other server.

  1. VueJS app is not NodeJS app.
  2. VueJS app is interpreted by the browser.
  3. You just have to build your app on computer and host files as any static website, so any server can serve html and files.
  4. To build your app use eg Webpack ( https://github.com/vuejs-templates/webpack )

NodeJs only use to build *.js files in front-end, your WebApp dosen't have to run on Nodejs.

1, You can create a index.html file that requires *.js file when webpack built it.

2, Use Chrome to open your index.html file so you can see it works.

You don't need to use vue-cli or other servers if you only want a static page.

But you have to know how to set your webpack.config.js, you can look that doc https://webpack.js.org/guides/getting-started/

Your starting point is wrong. Vue + node.js can build a complete site. Vue is the front-end framework, node's server language. The two can be used in combination. But not vue must rely on node to use. The two of them can be perfect to achieve the front and back separation of the development model.

In projects that use vue, individuals do not recommend configuring webpack and vue-loader separately. You can directly use vue official scaffolding, vue-cli. Do not have to consider these configurations, automatically configured.

Vue-cli

If you just started learning Vue, here's an entry-level demo. Although it is only a small application, but it covers a lot of knowledge points (vue2.0 + vue-cli + vue-router + vuex + axios + mysql + express + pm2 + webpack), including front-end, back-end, database and other sites Some of the necessary elements, for me, learning great significance, would like to encourage each other!

Vue Demo

Best way to develop Vue app is run dev server, and after all just build static assets. You don't need use vuex files, even better is use static template because you can easily integrate it with some back-end (WordPress or whatever). Helpfully will be use some starter, for ex. Vue.js starter

It's true that vue will create static html pages when you run the build script. However, you will need to serve the files from a small server for the site to work. If you notice, when you run npm run build , the terminal will print a notice...

Tip:
Built files are meant to be served over an HTTP server.
Opening index.html over file:// won't work.

You can create a simple http server in your /dist directory with express and then host your site somewhere like Heroku.

Take a look at this article https://medium.com/@sagarjauhari/quick-n-clean-way-to-deploy-vue-webpack-apps-on-heroku-b522d3904bc8#.4nbg2ssy0

TLDR;

  1. write a super simple express server

    var express = require('express'); var path = require('path'); var serveStatic = require('serve-static'); app = express(); app.use(serveStatic(__dirname)); var port = process.env.PORT || 5000; app.listen(port); console.log('server started '+ port);
  2. add a postinstall script in a package.json within /dist

    { "name": "myApp", "version": "1.0.0", "description": "awesome stuff", "author": "me oh my", "private": true, "scripts": { "postinstall": "npm install express" } }
  3. push only your /dist folder to heroku after you've compiled your site.

proof: I've followed these steps to host my vue.js project

Could you try something as simple as an S3 bucket setup for web serving? How big is your project? How much traffic do you think you'll get? If it's very small, you may be able to host on S3 and use webpack, etc.

using vue files without NodeJS (nor webpack) is possible with vue3-sfc-loader.

vue3-sfc-loader
Vue3/Vue2 Single File Component loader. Load .vue files dynamically at runtime from your html/js. No node.js environment, no (webpack) build step needed.

vue3-sfc-loader will parse your .vue file at runtime and create a ready-to-use Vue component.

disclamer: author here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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