E
E
Egor Sh2016-03-15 18:28:18
go
Egor Sh, 2016-03-15 18:28:18

Can GOlang be used to route Angular2 pages?

Please tell me, for example, I wrote a small web application in Angular2 with several pages, all routes are implemented inside Angular2 like this

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from 'angular2/router';
import {AuthComponent} from './auth.component';

@Component({
    selector: 'my-app',
    template: `
    <h1></h1>
    
    <router-outlet></router-outlet>
  `,
  
  directives: [ROUTER_DIRECTIVES],
  providers: [ ROUTER_PROVIDERS]
})

  @RouteConfig([
    { path: '/auth', name: 'Auth', component: AuthComponent, useAsDefault: true }
  
  ])

export class AppComponent { 
  
}

So in order for the routes to work and the page to return when I go to ip or domain name, I start the whole thing with the npm start command, it includes compiling TypeScript to JavaScript and running lite-server, which just allows you to route pages and check code update. I can somehow do without lite-server and use my GoLang backend so that pages in angular2 are routed and requests are made to the backend.
maybe something like this
http.Handle("/", http.FileServer(http.Dir("./app/web/")))
    http.ListenAndServe(":8080", nil)

where /app/web/ is the folder that contains my components and html templates or something like that.
How is this even implemented? in fact, I need to somehow replace lite-server with my GoLang back.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mr4x, 2016-03-16
. @EgorkZe

You can enable routing on the main page and use:

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "index.html")
})
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("./path/to/assets"))))

Then all requests will come to index.html, where you can process them, and the static will be loaded from another directory (assets)

D
Dmitry Voronkov, 2016-03-15
@DmitryVoronkov

You can do this:
Or this:
Js routing is not connected to the web server in any way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question