Closed diamondbroplayz closed 7 months ago
This worked for me the main modification is handelling of the bare server with fastify
const fastify = require('fastify')({ logger: false });
const fs = require('fs').promises; // Using promises for async/await
const path = require('path');
const { createBareServer } = require('@tomphttp/bare-server-node');
const bareServer = createBareServer('/bare/');
const readTemplateFile = async (filePath) => {
try {
const content = await fs.readFile(filePath, 'utf-8');
return content;
} catch (err) {
console.error(`Error reading file ${filePath}:`, err);
return '';
}
};
fastify.register(require('@fastify/static'), {
root: path.join(__dirname, 'static'),
prefix: '/', // optional: default '/'
});
fastify.use(bareServer.middleware);
const replaceContent = (fileData, meta, navbar) => {
return fileData.replace('<head>', meta + '<head>').replace('<body>', `<body> ${navbar}`);
};
(async () => {
const navbar = await readTemplateFile(path.join(__dirname, './templates/navbar.html'));
const meta = await readTemplateFile(path.join(__dirname, './templates/meta.html'));
try {
const files = await fs.readdir('./pages');
for (const file of files) {
const filePath = path.join(__dirname, './pages', file);
let fileData = await readTemplateFile(filePath);
fileData = fileData.replace('</head>', '\t' + meta + '\n</head>').replace('<body>', '<body>\n\t' + navbar);
fastify.get(`/${file.split('.')[0] === 'index' ? '' : file.split('.')[0]}`, (req, res) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, res);
} else {
res.status(200).type('text/html').send(fileData);
}
});
}
fastify.listen({ port: 3000 }, (err, address) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
} catch (err) {
console.error('Error reading pages directory:', err);
}
})();
This worked for me the main modification is handelling of the bare server with fastify
const fastify = require('fastify')({ logger: false }); const fs = require('fs').promises; // Using promises for async/await const path = require('path'); const { createBareServer } = require('@tomphttp/bare-server-node'); const bareServer = createBareServer('/bare/'); const readTemplateFile = async (filePath) => { try { const content = await fs.readFile(filePath, 'utf-8'); return content; } catch (err) { console.error(`Error reading file ${filePath}:`, err); return ''; } }; fastify.register(require('@fastify/static'), { root: path.join(__dirname, 'static'), prefix: '/', // optional: default '/' }); fastify.use(bareServer.middleware); const replaceContent = (fileData, meta, navbar) => { return fileData.replace('<head>', meta + '<head>').replace('<body>', `<body> ${navbar}`); }; (async () => { const navbar = await readTemplateFile(path.join(__dirname, './templates/navbar.html')); const meta = await readTemplateFile(path.join(__dirname, './templates/meta.html')); try { const files = await fs.readdir('./pages'); for (const file of files) { const filePath = path.join(__dirname, './pages', file); let fileData = await readTemplateFile(filePath); fileData = fileData.replace('</head>', '\t' + meta + '\n</head>').replace('<body>', '<body>\n\t' + navbar); fastify.get(`/${file.split('.')[0] === 'index' ? '' : file.split('.')[0]}`, (req, res) => { if (bareServer.shouldRoute(req)) { bareServer.routeUpgrade(req, res); } else { res.status(200).type('text/html').send(fileData); } }); } fastify.listen({ port: 3000 }, (err, address) => { if (err) { fastify.log.error(err); process.exit(1); } }); } catch (err) { console.error('Error reading pages directory:', err); } })();
Thank you so much!!
I'm making a game website with node.js (im using the fastify lib)
When I go to /bare/ it shows this: {"message":"Route GET:/bare/ not found","error":"Not Found","statusCode":404}
Help