I'm trying to upload my NodeJS express server to cdnapi.club through CPanel, It is using v16.17.1 and has DiscordJS, Express.
This is the error I am currently getting
Listening on port 3000
stderr:
/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1184
if (socket[kParser].timeoutType !== TIMEOUT_IDLE) {
^
TypeError: Cannot read properties of undefined (reading 'timeoutType')
at _resume (/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1184:29)
at resume (/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1148:3)
at connect (/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1133:3)
It starts listening to the actual port and then it crashes for some reason. I have been looking and trying to fix things for hours and haven't seen a fix. This is the index.js file for starting the server
const express = require('express');
const app = express();
const port = 3000
app.set('port', port)
const v1_path = "/api/v1"
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
app.get(`/`, (request, response) => {
response.sendFile(__dirname + '/pages/index.html')
})
This is the package.json https://pastes.dev/aUcTtdU0vX
Related
I have centos 6 running nodejs 10,
and i have this scaffold code to test my application server but still giving Error: connect ECONNREFUSED ip:4000
const express = require('express')
const app = express()
const port = 3432
const https = require('https');
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
When i try to put the url in the browser it says This site can’t be reached.
I like server is not running but it is running.
The error was that server administrator dont enable the port for me
I am trying to deploy my full stack application built using MySQL, Express, Angular and Node on Plesk Obsidian. However, I am facing an issue during the deployment. When my client side sends a request to the server then, I get the following error:
"Http failure response for https://example.com:3000/api/mytargetapi: 0 Unknown Error"
"GET ... net::ERR_CONNECTION_REFUSED"
Everything was working smoothly until I deployed it on Plesk.
Here's my code of the request on the client side:
const params = new HttpParams().set('id', this.searchInDirectory);
this.http.get('https://example.com:3000/api/mytargetapi', {params})
.subscribe(response =>
{
this.itemDisplay = response;
}
server side:
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});
router.get('/api/mytargetapi', (req, res) => {
stuff here...
}
Does anybody know what am I doing wrong? I have searched the same question on stack overflow and followed each and every individual's solution but nothing seems to be working for me. Any help would be appreciated. Thanks!
I don't know how you're organizing your project structure for the back-end API, but you should test the API via POSTMAN before integration with your front-end client
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.get('/api/mytargetapi', (req, res) => {
stuff here...
}
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});
I have been struggling to find the source of an error in the following code:
const express = require("express");
const path = require("path");
const WebSocket = require("ws");
const SocketServer = WebSocket.Server;
const PORT = process.env.PORT || 3000; // port to listen on
const INDEX = path.join(__dirname, "index.html"); // index address
var server = express();
server.use((req, res) => res.sendFile(INDEX));
server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
const wss = new SocketServer({server}); // wss = web socket server
(I have code below for connection etc. that is irrelevant to this question I think)
Upon running this, I receive the following error from the client in the browser:
WebSocket connection to 'ws://localhost:3000/' failed: Connection closed before receiving a handshake response
What is confusing me is that the code works if I make the following change:
var server = express()
.use((req, res) => res.sendFile(INDEX));
.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
The problem with this method is that it does not work to add in another .use before the existing .use, which is necessary for accessing static files (i.e. javascript as its own file instead of in an HTML file)
Why does changing my code between these two examples break it? What can I do to resolve this?
Thank you
You need to pass the http.createServer instance to the WebsocketServer. Not express instance
var app = express();
var server = http.createServer(app);
I found a solution!
It turns out that you must set the value of the server variable when .use and .listen are called.
var server = express();
server = server.use((req, res) => res.sendFile(INDEX));
server = server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
instead of
var server = express();
server.use((req, res) => res.sendFile(INDEX));
server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
I have an express server setup online which loads multiple ports and those ports are setup on subdomains for example. port 9000 loads the main domain.com port 8000 loads the main application at "app.domain.com" port 1000 loads "signup.domain.com" and the build version of the app is on port 8500 "build.domain.com".
The application is an Angular application however when I go to load the Angular app it loads on port 4200 or it says 8500 is in use. So currently I am loading that in express like so:
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
So my question is in Express how can I instead of writing sendfile command make it launch the angular app in that location on port 8500 so my subdomain names will work. The reason I'm asking this is because right now all it does is load the index file but angular or the app isn't running so i just see source code that says app-root and a blank white page.
Thank you in advance.
Robert
--- Update. I've decided to post the entire Express file. My issue is trying to load a angular app on port 8500 from the subfolder upon booting of express. Here is the full server.js code:
// server.js
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cors = require('cors'),
mongoose = require('mongoose'),
config = require('../config/DB');
mongoose.Promise = global.Promise;
mongoose.connect(config.DB).then(
() => {console.log('Database is connected') },
err => { console.log('Can not connect to the database'+ err)}
);
// Main Website
var web = express();
web.get('/', function (req, res){
res.sendFile('/web/index.html', { root: '.' })
});
var port = 9000;
web.listen(port);
console.log('Web Listening on port', port);
// Main Application
var app = express();
app.get('/', function (req, res){
res.sendFile('/app/index.html', { root: '.' })
});
var port = 8000;
app.listen(port);
console.log('Main App Listening on port', port);
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
// Sign up Portal
var sign = express();
sign.get('/', function (req, res){
res.sendFile('/signup/index.html', { root: '.' })
});
var port = 10000;
sign.listen(port);
console.log('Sign Up Portal Listening on port', port);
Refer to this link https://malcoded.com/posts/angular-backend-express
Update your code to the following:
const express = require('express');
const app = express();
app.listen(8500, () => {
console.log('Server started!');
});
You need to build the angular app if your angular version not 1.x
ng build
Also, I think this question is similar to your question:
Not able to view Angular app via express/heroku?
I am using Express 4.2.0 and node.js 0.10.12.
The weird thing is that I created a project in C\program files\node\nodetest and when I did npm start I got no errors.
Now I created a project in C\program files\node\secondtest and when I do npm start I get
app.set('port' , process.env.port 3000) typeerror object #<object> has no method 'set' at object.<anonymous> and its pointing in C\program files\node\secondtest\bin\www:5:5
Truth is , I dont know how to deal with this error, because I dont get what it means. Is it because both my projects listen on port 3000?
I just started secondtest , I installed succesfully the dependencies with npm install and added this in app.js
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Thanks
EDIT
If I leave the default code in app.js and www I get no errors. If I replace the default code of app.js with mine, and I remove the
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
part from www, then I get no errors.
Because I guess app.set and app.get are depricated in express 4.2.0? Or because when I set an http server in my app.js code, conflicts the default www code? Either one of these, or I am really confused.
EDIT 2
This is the default code of the www
#!/usr/bin/env node
var debug = require('debug')('secondtest');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
Updated answer according to the updated question.
Since you're calling www and its code needs to set the port and listen to it, your secondtest code should not listen to the port. Instead it should export the Express app as follows:
// ...
module.exports = app;
The www will do the listening part.
Otherwise, the secondtest tries to start listening on a port while not exporting the Express app, and www tries to listen again on a variable app which is not an Express app, thus the error object #<object> has no method 'set'.
When you do var app = require('../app'); in another script, it is important so that this ../app script actually exports the Express app.
Old answer.
Do node app.js instead of using npm command.
Second, make sure the same port is not used by both processes at the same time. You can't listen to the same port unless you're in cluster mode.
Considering the following is the content of both firsttest and secondtest:
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(process.env.port || 3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Do the following to start both apps:
Terminal 1: (the first app will default to port 3000).
$ node firsttest/app.js
Terminal 1:
$ export PORT=3001
$ node secondtest/app.js