-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): update app generator for fastify to be more inline with f…
…astify cli (#14667)
- Loading branch information
Showing
2 changed files
with
27 additions
and
15 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
packages/node/src/generators/application/files/fastify/src/app/app.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; | ||
|
||
/* eslint-disable-next-line */ | ||
export interface AppOptions { | ||
} | ||
|
||
export async function app(fastify: FastifyInstance, opts: AppOptions) { | ||
fastify.get('/', async function(request: FastifyRequest, reply: FastifyReply) { | ||
return { message: 'Hello API' }; | ||
}); | ||
} |
31 changes: 16 additions & 15 deletions
31
packages/node/src/generators/application/files/fastify/src/main.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
import fastify from 'fastify'; | ||
import Fastify from 'fastify'; | ||
import { app } from './app/app'; | ||
|
||
const host = process.env.HOST ?? 'localhost'; | ||
const port = process.env.PORT ? Number(process.env.PORT) : <%= port %>; | ||
|
||
const app = fastify(); | ||
|
||
app.get('/', async (req, res) => { | ||
return { 'message': 'Hello API'}; | ||
// Instantiate Fastify with some config | ||
const server = Fastify({ | ||
logger: true, | ||
}); | ||
|
||
const start = async() => { | ||
try { | ||
await app.listen({ host, port }); | ||
console.log(`[ ready ] http://${host}:${port}`); | ||
} catch (err) { | ||
// Errors are logged here | ||
process.exit(1); | ||
} | ||
} | ||
// Register your application as a normal plugin. | ||
server.register(app); | ||
|
||
start(); | ||
// Start listening. | ||
server.listen({ port, host }, (err) => { | ||
if (err) { | ||
server.log.error(err); | ||
process.exit(1); | ||
} else { | ||
console.log(`[ ready ] http://${host}:${port}`); | ||
} | ||
}); |