Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created April 16, 2019 22:01
Show Gist options
  • Save senthilmpro/2082fad37b7f94aa5ee23c18ab562009 to your computer and use it in GitHub Desktop.
Save senthilmpro/2082fad37b7f94aa5ee23c18ab562009 to your computer and use it in GitHub Desktop.
fastify-server setup
const fastify = require('fastify')({ logger: true });
const axios = require('axios');
const path = require('path');
const routes = require('./routes/route');
//console.log(route);
routes.forEach((route, index) => {
fastify.route(route)
});
// point to public folder.
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'archive-checker-v2',"dist",'archive-checker-v2')
//prefix: '/', // optional: default '/'
});
fastify.get('/', function (req, reply) {
reply.sendFile('index.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
})
// Run the server!
const start = async () => {
try {
await fastify.listen(3000)
fastify.log.info(`server listening on ${fastify.server.address().port}`)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
const HttpHelper = require('./../services/httphelper');
const routes = [
{
'method' : 'GET',
'url' : '/api/user/:name',
'handler' : async (req, reply) => {
const name = req.params.name;
const data = await HttpHelper.getHttpUrl(name);
return data;
}
}
]
module.exports = routes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment