Hello, I'm creating a REST API with Node.js (I started learning Node a few days ago). The point is that when I start the server, I can make one request. From the second request onwards, I get the following error:
{
"message": "Request error",
"error": {
"errno": -111,
"code": "ECONNREFUSED",
"syscall": "connect",
"address": "2600:9000:2123:6c00:c:174a:c400:93a1",
"port": 80
}
}
My code is as follows:
import express from 'express';
import https from 'node:http';
import { moviePropsFilter } from './utils/index.mjs';
const options = {
method: 'GET',
hostname: 'api.themoviedb.org',
port: null,
path: '/3/movie/popular?language=en-US&page=1',
headers: {
accept: 'application/json',
Authorization: here is my access token,
},
};
const app = express();
app.disable('x-powered-by');
app.get('/movies/popular', (req, res) => {
const request = https.request(options, (apiRes) => {
let data = '';
apiRes.on('data', (chunk) => {
data += chunk;
});
apiRes.on('end', () => {
if (apiRes.statusCode === 200) {
res.json(moviePropsFilter(JSON.parse(data).results));
} else {
res.status(apiRes.statusCode).json({ error: 'An error has occurred' });
}
request.end();
});
});
request.on('error', (err) => {
res.status(500).json({ message: 'Request error', error: err });
request.end();
});
});
app.listen(1234, console.log('Listening on 1234'));
I would like to know if you can help me with this, whether it's something I'm doing wrong in the code or if it's a connection problem on my PC. Help would be appreciated.
Kan du inte hitta en film eller tv-serie? Logga in för att skapa den.
Vill du betygsätta denna artikel eller lägga till den i en lista?
Inte medlem?