Introduction

Fastify is a web server framework similar to ExpressJS, but with better performance

image.png

The ecosystem is very complete, with many ready-made plug-ins. But in the first test, I only used the fastify-static plugin to get the .html file

Example

Initialize the project

First, create a void folder and install Fastify and fastify-static

npm i fastify fastify-static

app.js

Create an app.js, it is your root file
You can write the basis of this file to create 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)

HTML

Create a /public folder and an about.html file

image.png

Start

npm start
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部