Update readme

pull/6/head
Fenny 2020-01-16 10:51:56 +01:00
parent 658c4067bd
commit bc0547c169
2 changed files with 65 additions and 92 deletions

155
README.md
View File

@ -1,32 +1,35 @@
<img src="docs/static/logo.jpg" width="150" alt="Fiber"><br><br><span style="color:red"><b>IMPORTANT: Do not use this in production, API might change before we release v1.0.0!</span></b><br><br>
[![Latest Release](https://img.shields.io/github/release/gofiber/fiber.svg)](https://github.com/gofiber/fiber/releases/latest)
[![GoDoc](https://godoc.org/github.com/gofiber/fiber?status.svg)](http://godoc.org/github.com/gofiber/fiber)
[![Go Report](https://goreportcard.com/badge/github.com/gofiber/fiber)](https://goreportcard.com/report/github.com/gofiber/fiber)
[![GitHub license](https://img.shields.io/github/license/gofiber/fiber.svg)](https://github.com/gofiber/fiber/blob/master/LICENSE)
[![Join the chat at https://gitter.im/FiberGo/community](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/FiberGo/community)
<br><br>
<!-- **[Fiber](https://github.com/gofiber/fiber)** is a router framework build on top of **[FastHTTP](https://github.com/valyala/fasthttp)**, the fastest HTTP package for **[Go](https://golang.org/doc/)**.<br>
This library is inspired by **[Express](https://expressjs.com/en/4x/api.html)**, one of the most populair and well known web framework for **[Nodejs](https://nodejs.org/en/about/)**. -->
<p align="center">
<img height="150" src="https://gofiber.github.io/fiber/static/logo.jpg">
</p>
<!--
![](https://img.shields.io/github/issues/gofiber/fiber)
![](https://img.shields.io/github/stars/gofiber/fiber)
-->
**[Fiber](https://github.com/gofiber/fiber)** is an **[Express](https://expressjs.com/en/4x/api.html)** style HTTP framework implementation running on **[FastHTTP](https://github.com/valyala/fasthttp)**, the fastest HTTP engine for **[Go](https://golang.org/doc/)**. The package make use of similar framework convention as they are in expressjs. People switching from **[Nodejs](https://nodejs.org/en/about/)** to **[Golang](https://golang.org/doc/)** often end up in a bad learning curve to start building their webapps, this project is meant to ease things up, but with performance in mind (**Express on steriods**)
# Fiber ![](https://img.shields.io/github/release/gofiber/fiber) ![](https://img.shields.io/github/languages/top/gofiber/fiber) ![](https://img.shields.io/github/languages/code-size/gofiber/fiber) ![](https://godoc.org/github.com/gofiber/fiber?status.svg) ![](https://goreportcard.com/badge/github.com/gofiber/fiber)
## Full API Documentation
**[Click here](https://gofiber.github.io/fiber/)**
**[Fiber](https://github.com/gofiber/fiber)** is an **[Express](https://expressjs.com/en/4x/api.html)** styled HTTP framework implementation running on **[Fasthttp](https://github.com/valyala/fasthttp)**, the **fastest** HTTP engine for **[Go](https://golang.org/doc/)**. The package make use of similar framework convention as they are in express. People switching from **[Node](https://nodejs.org/en/about/)** to **[Go](https://golang.org/doc/)** often end up in a bad learning curve to start building their webapps, this project is meant to **ease** things up for **fast** development, but with **zero memory allocation** and **performance** in mind. See **[API Documentation](https://gofiber.github.io/fiber/)**
## Benchmarks
**[See all benchmarks](https://gofiber.github.io/fiber/#/benchmarks)**
[![](https://gofiber.github.io/fiber/static/benchmarks/benchmark.png)](https://gofiber.github.io/fiber/#/benchmarks)
![](https://gofiber.github.io/fiber/static/benchmarks/benchmark.png)
## Features
* Optimized for speed and low memory usage.
* Rapid Server-Side Programming
* Easy routing with parameters
* Static files with custom prefix
* Middleware with Next support
* Express API endpoints
* **[API Documentation](https://gofiber.github.io/fiber/)**
## Installing
Assuming youve already installed **[Go](https://golang.org/doc/)**, install the **[Fiber](https://github.com/gofiber/fiber)** package by calling the following command:
```shell
```bash
$ go get -u github.com/gofiber/fiber
```
## Hello world
Embedded below is essentially the simplest Fiber app you can create.
```shell
```bash
$ create server.go
```
```go
@ -36,98 +39,68 @@ import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
app.Get("/:name", func(c *fiber.Ctx) {
c.Send("Hello, " + c.Params("name") + "!")
})
app.Listen(8080)
}
```
```shell
```bash
$ go run server.go
```
Browse to **http://localhost:8080** and you should see Hello, World! on the page.
## Basic routing
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
Each route can have one handler function, that is executed when the route is matched.
Route definition takes the following structures:
```go
// Function signature
app.Method(func(*fiber.Ctx))
app.Method(path string, func(*fiber.Ctx))
```
* **app** is an instance of **[Fiber](#hello-world)**.
* **Method** is an [HTTP request method](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods), in capitalization: Get, Put, Post etc
* **path string** is a path or prefix (for static files) on the server.
* **func(*fiber.Ctx)** is a function executed when the route is matched.
This tutorial assumes that an instance of fiber named app is created and the server is running. If you are not familiar with creating an app and starting it, see the [Hello world](#hello-world) example.
The following examples illustrate defining simple routes.
```go
// Respond with Hello, World! on the homepage:
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})
//Respond to POST request on the root route (/), the applications home page:
app.Post("/", func(c *fiber.Ctx) {
c.Send("Got a POST request")
})
// Respond to a PUT request to the /user route:
app.Put("/user", func(c *fiber.Ctx) {
c.Send("Got a PUT request at /user")
})
// Respond to a DELETE request to the /user route:
app.Delete("/user", func(c *fiber.Ctx) {
c.Send("Got a DELETE request at /user")
})
```
Browse to **http://localhost:8080** and you should see `Hello, World!` on the page.
## Static files
To serve static files such as images, CSS files, and JavaScript files, replace your function handler with a file or directory string.
To serve static files, use the [Static](https://gofiber.github.io/fiber/#/?id=static-files) method.
```go
// Function signature
app.Static(root string)
app.Static(prefix, root string)
```
For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public:
package main
```go
app.Static("./public")
import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Static("./public")
app.Listen(8080)
}
```
Now, you can load the files that are in the public directory:
```shell
http://localhost:8080/images/kitten.jpg
http://localhost:8080/images/gopher.png
http://localhost:8080/css/style.css
http://localhost:8080/js/app.js
http://localhost:8080/images/bg.png
http://localhost:8080/js/jquery.js
http://localhost:8080/hello.html
```
To use multiple static assets directories, call the express.static middleware function multiple times:
```go
app.Static("./public")
app.Static("./files")
```
?>For best results, use a reverse proxy cache like [NGINX](https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/) to improve performance of serving static assets.
To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:
## Middleware
Middleware never has been so easy!
```go
app.Static("/static", "./public")
```
Now, you can load the files that are in the public directory from the /static path prefix.
```shell
http://localhost:8080/static/images/kitten.jpg
http://localhost:8080/static/css/style.css
http://localhost:8080/static/js/app.js
http://localhost:8080/static/images/bg.png
http://localhost:8080/static/hello.html
package main
import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Get("/api*", func(c *fiber.Ctx) {
c.Locals("auth", "admin")
c.Next()
})
app.Get("/api/back-end/:action?", func(c *fiber.Ctx) {
if c.Locals("auth") != "admin" {
c.Status(403).Send("Forbidden")
return
}
c.Send("Hello, Admin!")
})
app.Listen(8080)
}
```
## API Documentation
We created an extended API documentation including examples, **[click here](https://gofiber.github.io/fiber/)**
## License
gofiber/fiber is free and open-source software licensed under the [MIT License](https://github.com/gofiber/fiber/edit/master/LICENSE).
*Caught a mistake? [Edit this page on GitHub!](https://github.com/gofiber/fiber/blob/master/README.md)*

View File

@ -45,7 +45,7 @@
console.log(n)
}
});
}, 250)
},500)
//