Create a Fastify server

Introduction

Fastify is a web server framework like ExpressJS but with better performances.

The ecosystem is pretty cool, he adds multiple plugins. But in this first test, I only add fastify-static for getting .html files.

Let…


This content originally appeared on DEV Community and was authored by Thomas Bnt

Introduction

Fastify is a web server framework like ExpressJS but with better performances.

Benchmark on the website Fastify.io

The ecosystem is pretty cool, he adds multiple plugins. But in this first test, I only add fastify-static for getting .html files.

Let's code !

At the first time, create a void folder and install Fastify and fastify-static.

npm i fastify fastify-static

Create an app.js, it's your root file.

Into the app.js

You can write the basis of this file for creating a new Fastify server.

const path = require("path")
const f = require('fastify')({logger: false})

f.register(require('fastify-static'), {
    root: path.join(__dirname, 'public'),
    prefix: '/public/',
})

// In this example, when you get localhost:3000, ou have the time
f.get('/', (request, reply) => {
    reply.header('Content-Type', 'application/json')
    reply.send({hello: new Date()})
})
f.get('/about', (request, reply) => {
    reply.sendFile('about.html' )
})


const start = async () => {
    try {
        await f.listen(3000)
    } catch (err) {
        f.log.error(err)
        process.exit(1)
    }
}
start().then(r => r)

Public HTML pages

Create a /public folder and a about.html file.

Create your public folder

End

It's a very short post, but I demonstrate how to simply start a server with Fastify. As this is the first time I use it, there might be some errors. Don't hesitate to give me feedback in the comments ! ??

Getting Started with Fastify

Check my Twitter account. You can see many projects and updates. You can also support me on Buy Me a Coffee.


This content originally appeared on DEV Community and was authored by Thomas Bnt


Print Share Comment Cite Upload Translate Updates
APA

Thomas Bnt | Sciencx (2021-08-23T23:51:00+00:00) Create a Fastify server. Retrieved from https://www.scien.cx/2021/08/23/create-a-fastify-server/

MLA
" » Create a Fastify server." Thomas Bnt | Sciencx - Monday August 23, 2021, https://www.scien.cx/2021/08/23/create-a-fastify-server/
HARVARD
Thomas Bnt | Sciencx Monday August 23, 2021 » Create a Fastify server., viewed ,<https://www.scien.cx/2021/08/23/create-a-fastify-server/>
VANCOUVER
Thomas Bnt | Sciencx - » Create a Fastify server. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/23/create-a-fastify-server/
CHICAGO
" » Create a Fastify server." Thomas Bnt | Sciencx - Accessed . https://www.scien.cx/2021/08/23/create-a-fastify-server/
IEEE
" » Create a Fastify server." Thomas Bnt | Sciencx [Online]. Available: https://www.scien.cx/2021/08/23/create-a-fastify-server/. [Accessed: ]
rf:citation
» Create a Fastify server | Thomas Bnt | Sciencx | https://www.scien.cx/2021/08/23/create-a-fastify-server/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.