tomphttp/bare-server-node

Error: Unable to fetch Bare meta: 404 Not Found

gagedevs opened this issue · 4 comments

const Koa = require('koa');const app = new Koa();
const port = 80;
const http = require('node:https')
const createBareServer = require('@tomphttp/bare-server-node');
const colors = require('./colors.js');
var logger = require('koa-logger');
const sendfile = require('koa-sendfile')
const bare = createBareServer('/bare/')

app.on('request', (req, res) => {
    if (bare.shouldRoute(req)) {
        bare.routeRequest(req, res)
    } else {
        app(req, res)
    }
})
app.on("error", err => console.log(err));
app.on('upgrade', (req, socket, head) => {
    if (bare.shouldRoute(req)) {
        bare.routeUpgrade(req, socket, head)
    } else {
        socket.end()
    }
});

I am using Koa.js to try and update my backend, and this code works with Express. It shows the following error in inspect element:

uv.sw.js:1 Error: Unable to fetch Bare meta: 404 Not Found
    at Sh (BareClient.ts:132:9)
    at async Ih.request (BareClient.ts:230:3)
    at async Ih.fetch (BareClient.ts:364:10)
    at async s.fetch (uv.sw.js:1:1725)

If I try and change app to httpsServer, it shows "Cannot set headers after they are sent to the client.` in the terminal, instead of inspect element.

const options = {
	key: key,
	cert: cert
}
const httpsServer = https.createServer(options, app.callback()); // adds koa.js app to https server
const bare = createBareServer('/bare/')

httpsServer.on('request', (req, res) => {
    if (bare.shouldRoute(req)) {
        bare.routeRequest(req, res)
    } else {
        app(req, res)
    }
})
httpsServer.on("error", err => console.log(err));
httpsServer.on('upgrade', (req, socket, head) => {
    if (bare.shouldRoute(req)) {
        bare.routeUpgrade(req, socket, head)
    } else {
        socket.end()
    }
});
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:393:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at writeResponse (C:\Users\shuttle\Downloads\shuttle test\node_modules\@tomphttp\bare-server-node\dist\AbstractMessage.js:46:13)
    at Server.routeRequest (C:\Users\shuttle\Downloads\shuttle test\node_modules\@tomphttp\bare-server-node\dist\BareServer.js:163:48)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

Is Koa.js not supported or am I doing something wrong? Tell me if you need more information

Do you know how to use Koa?

You should try reading https://koajs.com/. It looks like they don't want you to use ctx.res.write() (for instance) and want you to use their own Request/Response objects. Therefore, bare-server-node can't properly/fully support Koa. You could try passing ctx.req and ctx.res to Bare though. Regardless, you're not even using Koa properly.

If you don't need any Koa-specific middleware, then use Express.

Edit: if you don't need any routing and you don't need any middleware other than serving static files, you could also use Node.js's vanilla, builtin HTTP server and use serve-static, which is what Express uses for express.static().

This isn't my full code, and yes I am using Koa properly. To use HTTPS with Koa (if that's what you're talking about), you need to create an httpsServer and their docs say that app.listen() is just httpServer.createServer(app.callback()).listen(). Other than that (and bare), all other imports are official Koa libraries. Thanks for letting me know that though, and I also just tried using Koa yesterday, so I am probably doing some stuff wrong.

@oHeckGage I was actually looking at your app.on('request') stuff, which either doesn't work in Koa or defeats the point of Koa.