tomphttp / bare-server-node

TompHTTP Bare server in the Node runtime
GNU General Public License v3.0
72 stars 125 forks source link

Need help ASAP #27

Closed diamondbroplayz closed 7 months ago

diamondbroplayz commented 11 months ago

I'm making a game website with node.js (im using the fastify lib)

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 '/'
})

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, socket, head)
                } 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);
    }
})();

When I go to /bare/ it shows this: {"message":"Route GET:/bare/ not found","error":"Not Found","statusCode":404}

Help

AbdullahDaGoat commented 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);
    }
})();
diamondbroplayz commented 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);
    }
})();

Thank you so much!!