定义路由

发布时间 2023-07-12 17:47:40作者: jjw
const routes = (fastify, options, done) => {
        fastify.post('/login', async (request, reply) => {
            return 'ok'
        })
       
        fastify.get('/getBooks', async (request, reply) => {
            return 'OK'
        })
       
        fastify.get('/getUsers', async (request, reply) => {
            return 'OK'
        })
   
   
        done()
    }
   
module.exports = routes

定义路由后,再引用

fastify.register(require('./routes/index'), { prefix: '/v1' }) //添加路由前缀
 
fastify.listen({ port: 8088, host: APP_IP}, (err, address) => {
    console.log(`app run ip: ${APP_IP}`);
    if (err) throw err
    // lServer is now listening on ${address}
})

注册路由里可以定义路由前缀。