Node - Attaching to server / websockets - node.js

I am running a vue application (VUE-CLI 3) on Node. In dev mode, I run 'npm run serve' and the application is brought up and works as expected.
I would now like to add websocket code to the server.
Most examples I see have some setup code similar to:
const http = require('http');
const server = http.createServer();
const wsServer = new WebsocketServer({httpServer: server});
When I run 'npm run serve' I now am greeted with the following error message:
'http.createServer is not a function'
Is there a way to attach the websockets to the current running node server when invoked via npm run serve? In other words, can I skip the createServer call and attach it to whatever is currently running?

Well, your code should read something like this...
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
You will need to use ExpressJS on the back-end to handle messages.

Related

socket.io/socket.io.js cannot be found

I have this socket server:
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
...
app.listen(3001);
Express and socket versions:
"express": "^4.16.3",
"socket.io": "^2.1.1"
And I call this on client side:
<script src="http://localhost:3001/socket.io/socket.io.js"></script>
When I run socket server and open client app on chrome, I get this message: "Failed to load resource: the server responded with a status of 404 (Not Found)".
I don't intent to copy socket.io.js to a public folder as it's not the right way to do it according to socket.io docs.
That said, what am I doing wrong?
Possible fixes:
Make sure you run npm install and you don't have errors or incomplete installations:
inside your project's directory, go to ./node_modules/socket.io/node_modules/socket.io-client/dist and copy the file named socket.io.js and put it into one of your static or resources directory.
Don't forget put the following line to serve static files, now you should be able to browse it under: http://localhost:3000/resources/socket.io/socket.io.js
app.use('/resources', express.static('resources'));
If not fixed, here is a complete basic example follow the steps below.
Copy paste the following into a file myfile.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
http.listen(3000, function(){
console.log('listening on *:3000');
});
Run the following commands:
$ npm i express --save
$ npm i socket.io --save
$ node myfile.js
Open your browser http://localhost:3000/socket.io/socket.io.js

When running script for express web server with node on windows get prompt to select app

I'm going through this this course currently.
There is a tutorial on how to configure and run express server. The following script is suggested:
var express = require('express');
var path = require('path');
var open = require('open');
var port = 3000;
var app = express();
app.get('/',function(req, res){
res.sendFile(path.join(__dirname,'../src/index.html'));
});
app.listen(port,function(err){
if(err){
console.log(err);
}
else{
open('localhost:'+port);
}
});
When I run it in project root with $ node buildScripts/srcServer.js I get a system prompt You'll need a new app to open this localhost with the only suggestion being look at windows store.
What's that about? What does it need an app for? In the course when the script is run the browser is opened, but it's on Mac there. When I navigate manually to localhost:3000 there is the error like there is supposed to be, but I'm somewhat concerned that this behavior will mess with live reloading so I'd like to get rid of it.
Add the http:// prefix and it will open using the default browser.
open('http://localhost:'+port);

How to deploy my nodeJS application on wildfly server

I have developed server Rest APIs using node and ExpressJS. Now I want to deploy this application on wildfly server so that my Client application can access the APIs.
What is way to achieve this? below is my app.js file.
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var routes = require("./routes/routes.js")(app);
var server = app.listen(3000, function () {
console.log("Listening on port %s...", server.address().port);
});
You can try heroku cloud, it's simple and free untill some limits but it's nice for testing
link
If you have vps you can:
Deploy code somewhere for example github
Connect to your serwer by ssh
Install node.js on server
Pull your api code from github
Install app dependencies (npm)
Run app

Solving a Proxy Error (Node.js server)

Basically I get a 502 Proxy Error when running my node.js app.
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /play.
Reason: Error reading from remote server
My server looks like this.
var express = require('express');
var https = require('https');
var http = require('http');
var path = require('path');
var fs = require('fs');
var mysql = require('mysql');
var app = express();
var options = {
key: fs.readFileSync('sslcert/keyfile.key', 'utf8'),
cert: fs.readFileSync('sslcert/crtfile.crt', 'utf8')
};
var httpsServer = https.createServer(options, app);
// stuff
httpsServer.listen(process.env.PORT);
I am really sorry if this is a noob question, actually I am still a beginner in things related to node.js. Thank you for your help!
Noël.
I just ran into the same problem. It's possible your problem was different, but in my case, the 502 error was coming from Apache. My httpd.conf file was referencing the same 2 certificate files that my node app was referencing.
Instead of using
var httpsServer = https.createServer(options, app);
try just
app.listen(3000);
I'm not sure exactly why it wasn't working. My theory is the node app was using these cert files, and apache was unable to access them, and thus creating this situation. However, just using a normal express app fixed the problem for me.
I hope this helps somebody!

json-server 0.8.12 and Express 4.x Routing

I have an existing node-express project running on express using express server and have middlewares and routing on express
var express = require('express');
var app = express();
require('/path/to/express_conf_file')(app);
app.listen(config.port);
I want to use json-server for easy mocking and I've followed this. My current code looks like this:
var express = require('express');
var app = require('json-server');
var middlewares = express();
var server = app.create(); // Returns an Express server
**var router = server.router('db.json');** // Returns an Express router
server.use(middlewares);
server.use(router);
require('./config/express')(middlewares, config);
server.listen(4000);
server.route('db.json') seems to be deprecated in Express 4.x. What needs to be done to use 'db.json' with express 4.x?
I am invoking my application and json-server using concurrenrly using npm start and in package.json I've defined:
"js-server": "json-server --watch db.json --port 4000",
"start": "concurrently \"gulp command\" \"npm run js-server\""
Can somebody please advice as what should be the correct way of using json-server with Express 4.x?
It is solved here. json-server can be mounted at a certain path within express server. No need to start the json-server separately now. Only gulp command can be used to launch express server.
The json server can be hosted separately also and can be accessed through the specified port from different appilication(s).

Resources