JavaScript Node only sends html content to the client instead of sending a whole Vue app

I created a new vue project with the CLI and want to deploy it. Based on this documentation

https://router.vuejs.org/guide/essentials/history-mode.html#html5-history-mode

I added the history mode to the router. After running npm run build I took the example code for a static native Node server

https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

const http = require('http')
const fs = require('fs')
const httpPort = 3000

http.createServer((req, res) => {
  fs.readFile('../base/index.html', 'utf-8', (err, content) => {
    if (err) {
      throw err;
    }

    res.writeHead(200, {
      'Content-Type': 'text/html; charset=utf-8'
    })

    res.end(content)
  })
}).listen(httpPort, () => {
  console.log('Server listening on: http://localhost:%s', httpPort)
})

So when navigating to localhost:3000 the vue project seems to load correctly

enter image description here

but I have a blank page with two errors

enter image description here

When I click on those js files it shows me the content of the index.html file. Obviously js is not able to understand this html content. How can I fix that problem?

Answer:1

Server will not send the whole vue app at once.

  • Browser get html file from server, when you browse to that url.
  • Browser parse the html file.
  • Browser detects assets (js, images, css).
  • Browser request those files.

It request those file from server, but you haven't initialized server to find those files.

So, need to add static files.

https://expressjs.com/en/starter/static-files.html

You can take reference from here

Answer:2

I want to trying adding class using javascript, I know there is a lot of topic about this in stackoverflow but some of them is too complicated for me to understand, let say I have this simple code ...

I want to trying adding class using javascript, I know there is a lot of topic about this in stackoverflow but some of them is too complicated for me to understand, let say I have this simple code ...

  1. class javascript use strict
  2. javascript class using function
  3. javascript class using prototype
  4. class using javascript
  5. class in javascript
  6. class in javascript example
  7. javascript class with functions
  8. javascript class with constructor
  9. javascript class with properties
  10. javascript class with methods
  11. javascript class with multiple constructors
  12. javascript class with static methods
  13. javascript class with async function
  14. javascript class with array
  15. javascript class with variables
  16. javascript class with parameters
  17. javascript class in html
  18. javascript class in namespace
  19. javascript class with array property
  20. javascript class with arrow function

I have two variables with JSON files. The first is a list of keys looks like this: keylist = ["key1","key2","key3"] The second one is generated from a database and looks like this: data = { "...

I have two variables with JSON files. The first is a list of keys looks like this: keylist = ["key1","key2","key3"] The second one is generated from a database and looks like this: data = { "...

  1. access array with pointer c++
  2. access array with pointer
  3. access array with key javascript
  4. access array with index php
  5. access array with python
  6. access array of objects javascript
  7. access array in javascript
  8. access array in php
  9. access array of objects
  10. access array in matlab
  11. access array from another class java
  12. access array of json objects javascript
  13. access array in jquery
  14. access array in json
  15. access array by index javascript
  16. access array in c
  17. access array in dictionary python
  18. access array in struct c
  19. access array in java
  20. access array in scala

I have an array like below and I want to extract objects and sub-array and create a new array if the selected object value is true menu: [ { category_name: "snacks", selected: true ...

I have an array like below and I want to extract objects and sub-array and create a new array if the selected object value is true menu: [ { category_name: "snacks", selected: true ...

I have included javascript for dynamically adding input fields but the first time its clicked it doesn't style correctly. Here is how it looks before utilizing add button (ignore the Author 1/...

I have included javascript for dynamically adding input fields but the first time its clicked it doesn't style correctly. Here is how it looks before utilizing add button (ignore the Author 1/...

  1. dynamic adding new input field
  2. dynamic adding new input field ionic