Cannot connect node.js and socket.io to my front end - javascript

I am trying like this, but anyway see only cannot get /
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
app.use(express.static(path.join(__dirname+"/public")))
server.listen(5000);

Related

Error: connection.once is not a function (Javascript)

I am writing a code that allows me to connect to the MongoDB database that I had made. For some reason, I am getting errors connecting my page and I don't know why. I have already looked at TypeError: connection.once(...).catch is not a function for help but that page did not answer my question. I also copied the code that my professor made and I can't seem to get it working.
This is my code:
const express = require('express');
const app = express();
const connection = require('./db/connection.js');
const dotEnv = require('dotenv').config();
connection.once('open', ()=>{
const server = app.listen(process.env.PORT || 8080, ()=>{
console.log("Connected and listening");
});
});
app.use(express.static('public'));
app.use(express.urlencoded({extended:true}));
const Film = require('./models/film.js');
And this one is the connection.js file
const mongoose = require("mongoose");
let mongoDB = `name of database (this is correct, I used this to connect to a different project and it worked`;
module.exports = mongoose.connect(mongoDB);

configure socket.io to wss instead of ws

I am working on a small application that is hosted via node.js and uses socket.io for quick communication. For now it was only in development so it was fine. However now i would like to switch from ws:// to wss:// but dont really know where to start. I tried to research online but there is not much on the topic.
index.js (server)
const path = require("path");
const express = require("express");
const app = express();
const cors = require("cors");
const config = require("./config");
const webRoute = require("./lib/routes/web");
const widgetsRoute = require("./lib/routes/widgets");
const errorsHandler = require("./lib/handlers/errors");
const corsHandler = require("./lib/handlers/cors");
app.use(cors(corsHandler));
app.use(express.static(path.join(__dirname, "/public")));
app.use("/", webRoute);
app.use("/widgets", widgetsRoute);
app.use(errorsHandler);
app.listen(4118, "0.0.0.0");
const io = require("socket.io").listen(
require("http")
.createServer()
.listen(4113, "0.0.0.0")
);
require("./lib/inits/socket")(io);
client:
this.socket = io("http://example.org:4113");
In the network tab i can see that socket.io is using ws:// in one of its calls.
How can i modify this so it uses wss instead?

unit test for web socket in node is it possible?

im running unit test using mocha/supertest/chai and I want to test the following code so my question how should I simulate the upgrade event?
const http = require('http');
const app = require('./app');
const server = http.createServer(app);
var webSocket = function (server) {
server.on('upgrade', function (req, socket, head) {
//I want to enter here...

In node.js do I need http when building a socket.io/express app?

I have just started using node.js and I can build a simple app that responds to requests and has some basic routing using the express framework.
I am looking to create something using socket.io but I am slightly confused over the use of the 'http' module. I understand what http is but I don't seem to need it for the following to work:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.htm');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
I can serve a html page over http without requiring the http module explicitly with something such as:
var http = require('http');
If I am using express do I have any use for the http module?
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
...
server.listen(1234);
However, app.listen() also returns the HTTP server instance, so with a bit of rewriting you can achieve something similar without creating an HTTP server yourself:
var express = require('express');
var app = express();
var socketio = require('socket.io');
// app.use/routes/etc...
var server = app.listen(3033);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
...
});
source
http://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen
no, you probably don't need it.
You can use something like:
var app = require('express').createServer();
var io = require('socket.io')(app);
//Your express and socket.io code goes here:

Accessing io object in app.js

I have an application built on express. I have an app.js that starts the app, and a functions.js where I have stored a lot of the functionality of the app. I would like to be able to access the socket.io object to use in my functions.js. However, it seems that I cannot do this and I can't find any solution on the internet either.
app.js:
var app = express(); //var app = express.createServer();
var http = require('http').createServer(app).listen(3001);
var io = require('socket.io')(http);
functions.js:
//something like io = require('./app').io
I want to be able to access the io object so I can emit messages to my client side javascript.
Use module.exports to make the io object available to other modules.
app.js:
var express = require('express');
var app = express();
var http = require('http').createServer(app).listen(3001);
var io = require('socket.io')(http);
app.io = io;
module.exports = app;
require('./functions')
functions.js:
var io = require('./app').io;
io.on('connection', function(socket) {
console.log('a user connected');
);

Categories

Resources