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
Related
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.
I'm sorry to ask such a simple question. I've been sent files by someone, an index.html file which pulls in a js file within script tags. I have to start a webserver to get through authentication and view the files (am in dev).
In my CLI i have navigated to the directory containing index.html. I have checked with node -v that I have it installed globally (yes, v 8.6). I've run the simple command node and checked my browser at http://localhost:3000 and a few other ports but get no joy. I've also tried node index.html but CLI throws an error.
How do i start the webserver? All the examples online tell me to build a .js file, but this is not an option.
Steps to set up a node web server
Create the route folder from your local machine.
Go to the command prompt from the project root path.
Install express using the command npm install express
Create server.js file
create the folder wwww and create the Index.html inside it.
server.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/www'));
app.listen('3000');
console.log('working on 3000');
Index.html
<!doctype html
<html>
<head>
<title> my local server </title>
</head>
<body>
<h1> server working </h1>
<p> just put your html,css, js files here and it work on your own local nodejs server </p>
</body>
</html>
Go to the project root path and take the command prompt, then start the server by running the command node server.js
Then go to the browser and run the url localhost:3000.
Now you can see the html page will render on your browser.
Since you don't want to build a backend but just an http server.
I would propose to use an npm package that do just what you need:
Open a console
npm install http-server -g
Go to your "index.html" folder (in the console) then type:
http-server
Then reach your content in your browser at this address:
http://localhost:8080
Documentation here:
https://www.npmjs.com/package/http-server
Yes, this is possible.
A very simple example of how to do this would be to create file, let's call it app.js and put this in it:
const http = require('http'), // to listen to http requests
fs = require('fs'); // to read from the filesystem
const app = http.createServer((req,res) => {
// status should be 'ok'
res.writeHead(200);
// read index.html from the filesystem,
// and return in the body of the response
res.end(fs.readFileSync("index.html"));
});
app.listen(3000); // listen on 3000
Now, run node app.js
Browse to http://localhost:3000
There's loads of other npm packages that will help you out do this, but this is the simplest 'pure node' example to literally read index.html and serve it back as the response.
Its very easy to start a server using node js
Create a server.js file,
const http = require('http')
const fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(3000);
Run node server.js
Here is a reference
This will even solve your backslash issue by this
I have installed meteor library.
I want to run an angular project which has no back-end (of static content).
I want to create a server file using node.js for static content.
Is it possible to to create that and execute ?
There is a very simple example of how to create a static server in Node.js that server static content pages,
the following code is in the myserver.js file:
var http = require('http');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic("./");
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
server.listen(8000)
You need to install through NPM from command line:
$ npm install finalhandler serve-static
$ node myserver.js
It's possible with Meteor, but it's an overkill.
remove the default mongo package ($ meteor remove mongo)
put all your static files into a folder called public
You're done. This way your production build won't require a MongoDB server.
But it's a lot easier to just use the http-server NPM package to setup a static file server with Node.js.
I am trying to learn the basics of node.js and socket.io. I have been using this tutorial http://tutorialzine.com/2012/08/nodejs-drawing-game/
the full code for this problem can be seen in the link above.
I can create a basic web server with node.js and get it to return hello world so I am sure that's installed correctly. However upon installing these packages
npm install socket.io#0.9.10 node-static
and setting up the serverside js as instructed
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
var fileServer = new nstatic.Server('./');
app.listen(8080);
I just get this prompt in my cmd and a constantly hanging web browser, instead of the html page that is meant to be served.I think I may have messed up an install but upon looking at the list of installed packages in npm it states both socket.io and node-static are present.
The code below should be more effective?, it looks like you are missing the handler part. The response must be explicitly ended or browser requests will hang forever like you are seeing. The node-static file.serve method manages the request once you pass it down. The source for .serve is here: https://github.com/cloudhead/node-static/blob/master/lib/node-static.js#L164
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static');
app.listen(8080);
var file = new nstatic.Server('./');
function handler(request, response) {
request.addListener('end', function () {
file.serve(request, response);
}).resume();
}
console.log('started')
Note also that the default file to serve to responses at / is index.html.
My sio = require('socket.io').listen(app) is in my server.js file, but I'm calling a method in a library that would like to push a message to the client... say api.user.pushToClient()
How am I able to access sio.sockets from there? Perhaps my structure is incorrect?
Folder structure:
server.js
api
|--user.js
|--another.js
in server.js append this line
module.exports.sio = sio;
in api/user.js
sio = require('../server').sio;
sio.sockets.on ...
Or did I misunderstand the question?
What I understood from the question is you want to know how to use socketIO with node module.Based on my understanding you can use it as below:
First install socketIO module locally with npm by running " $npm install socket.io " command for windows.
Add Script to your HTML page:
<script src="/socket.io/socket.io.js"></script>
Now add var io = require('socket.io'); to your server or js file where you are going to use it.
Then you can have server startup code listen to that server and on connection of it perform the options for any event.
var listener = io.listen(server);
listener.sockets.on('connection', function(socket) {
socket.on('locationClick', function(data) {
// perform the function on receving locationClick event.
}
}