loading...
Cover image for Welcome to Fiber — an Express.js styled web framework written in Go with ❤️

Welcome to Fiber — an Express.js styled web framework written in Go with ❤️

koddr profile image Vic Shóstak Updated on ・3 min read

Introduction

Hello, World! 👋 Today we will deal with a young (but ambitious) Fiber web framework on Go and understand that this is not "yet another new framework, like X", but a great tool for creating rapid web applications.

📌 It will be a review article, dive into Fiber we will start further.

Table of contents

  1. What is Fiber and why is it so good?
  2. Useful information to start working with Fiber
  3. Main features
  4. Benchmarks
  5. Project assistance

What is Fiber and why is it so good?

Follow official README from GitHub repository:

Fiber is an Express.js styled HTTP web framework implementation running on Fasthttp, the fastest HTTP engine for Go (Golang). The package make use of similar framework convention as they are in Express.

And I tend to agree with that. If you have ever implemented a web application on Node.js using Express.js (like me), then many methods and principles will seem very common to you!

For example, this is standard Hello, World! by Express.js:

// ...

const app = express()

app.get('/', (req, res) => res.send('Hello, World!'))

app.listen(8080)

And similar example by Fiber:

// ...

app := fiber.New()

app.Get("/", func(c *fiber.Ctx) {
  c.Send("Hello, World!")
})

app.Listen(8080)

Useful information to start working with Fiber

Actually, all you need for start is official documentation! 😉

Fiber, as a web framework, was created with idea of minimalism to more easily start creating a web application's backend for new gophers, but who have experience with JavaScript.

That's what the authors themselves say:

People switching from Node.js to Go 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.

main features

Main 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
  • Extended documentation

Easy to enable the prefork feature

Just set Prefork to true on your code:

// ...

app := fiber.New()

app.Prefork = true // enable prefork

app.Get("/", func(c *fiber.Ctx) {
  c.Send(fmt.Sprintf("Hi, I'm worker #%v", os.Getpid()))
  // => Hi, I'm worker #16858
  // => Hi, I'm worker #16877
  // => Hi, I'm worker #16895
})

app.Listen(8080)

What's prefork?

Enable prefork feature will spawn multiple go processes listening on the same port. Nginx has a great article about Socket Sharding, this picture are taken from the same article 👇

prefork

✨ My favorite killer feature ✨

And one more big feature (for me) is full access to all Fasthttp methods and properties (read documentation for more info about it).

Yeah, you didn't mishear! Fiber is extremely easy to use as Express.js and has everything under the hood that Fasthttp has now and will have in the future 🔥

Benchmarks 🤖

Click here to see all benchmark results. I'll only bring some.

  • TechEmpower: JSON serialization

JSON serialization

  • Go-Web: enable HTTP pipelining

enable http pipelining

Project assistance

  1. Add a GitHub Star to project.
  2. Tweet about project on your Twitter.
  3. Help to translate README and API Docs to another language.

Photo by

[Title] Fiber Authors https://fiber.wiki
[1] Nate Grant https://unsplash.com/photos/dFF8z3WH5FI

P.S.

If you want more — write a comment below & follow me. Thx! 😘

Discussion

pic
Editor guide
Collapse
davidmz profile image
Давид Мзареулян

Why do we need prefork in Go? Isn't regular gorutines enough?

Collapse
fenny profile image
Fenny

Preforking is making use of multiple socket listeners on OS level. We explained preforking with images in our docs fiber.wiki/application#prefork

Collapse
davidmz profile image
Давид Мзареулян

Does prefork mean that workers are executed in different processes (not goroutines) and cannot communicate or share memory with each other?

Thread Thread
fenny profile image
Fenny

This is correct, preforking is for specific use cases. If you work with an external database it should be no problem, because you can pool connections. You can see a good result here: techempower.com/benchmarks/#sectio...

Collapse
orenmizr profile image
Oren Mizrahi

How is it compared to expressJS - connections benchmarks-wise ? (i'm sure it is better, but how much better)

Collapse
fenny profile image
Fenny

Plaintext responses per second
Fiber - 6,114,300/s (0 errors) 0.5ms avg latency
Express - 261,708/s (59 errors) 608.9ms avg latency
techempower.com/benchmarks/#sectio...

JSON responses per second
Fiber - 1,212,833/s (0 errors) 0.1ms avg latency
Express - 244,061/s (0 errors) 1.1ms avg latency
techempower.com/benchmarks/#sectio...

20 database queries per request
Fiber - 19,757/s (0 errors) 25.6ms avg latency
Express - 4,259/s (0 errors) 118.4ms avg latency
techempower.com/benchmarks/#sectio...

Keep in mind that these benchmarks are 1 month old and fiber is being updated on a daily basis. Fiber v1.8 will have better performance.

I will update this comment when techempower tested v1.8.

Collapse
ben_schoeffmann profile image
Benedikt Schöffmann

Hey Vic,

thanks for this article! I wanted to dive into Go for a long time, this is the kick in the butt I needed :)

Cheers,
Ben

P.S.: My first ever Dev.to posting.

Collapse
koddr profile image
Vic Shóstak Author

This is very cool! Glad I could move you.

Collapse
josephmbassey profile image
Joseph Michael

This is awesome, coming from a nodejs/expressjs background, will make it easy to understand and use Go. 👍

Some comments have been hidden by the post's author - find out more