Web 模块

Here we are all enabled in programming mode, i.e. there will be no dependency on configuration files. Please refer to the module’s configuration description and configuration examples for almost all parameters that use configuration files.

Quickly Start

Building a web server is as simple as writing the following in the code:

  1. import (
  2. "github.com/beego/beego/v2/server/web"
  3. )
  4. func main() {
  5. // now you start the beego as http server.
  6. // it will listen to port 8080
  7. web.Run()
  8. }

In this case, the web server will use port 8080, so please make sure that port is not occupied before starting.

Web examples

Basic Usage

Port

If you wish to specify the port of the server, then you can pass in the port at startup time:

  1. import (
  2. "github.com/beego/beego/v2/server/web"
  3. )
  4. func main() {
  5. // now you start the beego as http server.
  6. // it will listen to port 8081
  7. web.Run(":8081")
  8. }

Host And Port

We generally do not recommend this writing style as it does not seem necessary, i.e:

  1. import (
  2. "github.com/beego/beego/v2/server/web"
  3. )
  4. func main() {
  5. // now you start the beego as http server.
  6. // it will listen to port 8081
  7. web.Run("localhost:8081")
  8. // or
  9. web.Run("127.0.0.1:8081")
  10. }

If you just specify the host, but not the port, then we will use the default port 8080. For example:

  1. import (
  2. "github.com/beego/beego/v2/server/web"
  3. )
  4. func main() {
  5. // now you start the beego as http server.
  6. // it will listen to port 8080
  7. web.Run("localhost")
  8. }

Reference

Register routers