I've integrated for my bot APIs. I expected they will work very well, but the truth is no...
I still can't figure out how the server gonna abort itself...
This is error output, and the code, screenshot. Just help me...
Error: Request aborted
at onaborted (/home/runner/TModeration-Server/node_modules/express/lib/response.js:1052:15)
at Immediate._onImmediate (/home/runner/TModeration-Server/node_modules/express/lib/response.js:1094:9) {
code: 'ECONNABORTED'
}
// API server
const express = require('express')
const path = require("path")
const app = express()
const PORT = 443
app.get('/api/', (req, res) => {
var options = {
root: path.join(__dirname)
};
res.writeHead(200, {'Content-Type': 'text/plain'})
const requiredFile = req.path.toLowerCase.replaceAll('https://', '').replaceAll('tmoderation-server.henry133.repl.co', '').replaceAll('/api/', '')
res.sendFile(`./APIs/${requiredFile ?? "index.js"}`, options, function(err) { console.log(err) })
res.end()
})
app.get('/', (req, res) => {
var options = {
root: path.join(__dirname)
};
res.writeHead(200, {'Content-Type': 'text/html'})
res.sendFile("./html.txt", options, function(err) { console.log(err) })
res.end()
})
app.listen(PORT || 443, () => {
console.log(`APIs [BETA] is listening ${PORT}`)
})
CodePudding user response:
If you want to send a static file you can use sendFile method, try this syntax with path.resolve:
app.get('/', (req, res) => {
res.status(200).sendFile(path.resolve('html.txt'));
})
CodePudding user response:
I think this error occurs because the next middleware method is not called, try this syntax:
app.get('/', (req, res, next) => {
var options = {
root: path.join(__dirname)
};
res.writeHead(200, {'Content-Type': 'text/html'})
res.sendFile("./html.txt", options, function(err) {
if (err) {
next(err);
} else {
console.log('Sent file');
next();
}
})
res.end()
})