D
D
Dastan2017-02-17 11:26:13
JavaScript
Dastan, 2017-02-17 11:26:13

SSR on Express.js and vue.js. How does server.get('*', function (request, response){} work?

I'm interested in this line:

// Handle all GET requests
server.get('*', function (request, response) {
  // Render our Vue app to a string
  renderer.renderToString(
    // Create an app instance
    require('./assets/app')(),
    // Handle the rendered result
    function (error, html) {
      // If an error occurred while rendering...
      if (error) {
        // Log the error in the console
        console.error(error)
        // Tell the client something went wrong
        return response
          .status(500)
          .send('Server Error')
      }
      // Send the layout with the rendered app's HTML
      response.send(layout.replace('<div id="app"></div>', html))
    }
  )
})

How it works?
What I'm guessing (correct if that): Here the site is launched "as if in a virtual machine" and the VueJS application is rendered and the finished html is transferred to the client.
I can't figure out how API requests are processed.
Complete server.js code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima, 2017-02-19
@v_m_smith

Is this the code from here? ;) https://vuejs.org/v2/guide/ssr.html
This is just a demo, requests are not processed in it, for any line [because get('*', ] in the URL the server will return the same HTML.
To handle specific requests, you need to add new server.get('/requestXXX', etc.
see expressjs.com/ru/guide/routing. html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question