Home  >  Q&A  >  body text

javascript - Separate front and back, node serves as the middle layer, how should routing be written?

For example, the backend provides such an interfacehttp://wx.lxjjz.cn/do?g=api&m=hd&a=works-list.

Then my front-end ajax should be written like this

$.ajax({
  url:'/do?g=api&m=hd&a=works-list',
  success:(data)=>{
       console.log(data)
  }
})

Is this the correct way to write node routing? How should I write some interface parameters? How does node send a request to the backend interface and then pass the data to the template?

app.get( '/do', function( req, res ){
    res.render( 'index.ejs' ); //请求跳转到 index.ejs文件
})
仅有的幸福仅有的幸福2691 days ago1061

reply all(1)I'll reply

  • 黄舟

    黄舟2017-06-29 10:11:32

    Node serves as the middle layer, which means that node serves as both a server and a client. As a client, it initiates an HTTP request to the back-end interface, and as a server, it obtains the back-end data and renders it or directly returns JSON

    In addition, before and after separation, Node must obtain the back-end data, formulate a reasonable routing address, return JSON data, and use AJAX to request the node address at the browser level

    Browser (ajax) -> Middle layer (node) -> SERVER backend

    Node carries the tasks of page rendering and routing, and the SERVER backend carries the data processing tasks

    reply
    0
  • Cancelreply