naik3 naik3 - 5 months ago 29
Node.js Question

Error running and express-nodejs setup

I was working on nodejs executing python scripts using spawn and methods. I am getting output on the console. but I am not able to display it on the browser. It is showing error.I have pasted the error below. Can any one please help me in solving this problem. I have got this example from this stackoverflow
Here i am pasting my code:

import random, time
for x in range(10):
print(str(random.randint(23,28))+" C")


var express = require("express");
var path = require('path');
var bodyParser = require('body-parser');
var fs = require('fs');
var spawn = require('child_process').spawn;
var http = require('http').Server(app);
var io = require('')(http);

var app = express();
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
var pathtoScript = spawn('python', ["../External_scripts/"]);
pathtoScript.stdout.on('data', function (output) {
var val = String(output);
io.sockets.emit('response', { data: val});
var server = app.listen(8082,'', function () {

var port = server.address().port

console.log("App is listening at %s", port)


And index.html page

<!doctype html>
<title>Live temperature</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<div id="liveTemp">Loading...</div>

<script src=""></script>
<script src=""></script>
var socket = io();


I am getting this error

GET http://localhost:8082/ 404 (Not Found)
GET http://localhost:8082/ 404 (Not Found)


You haven't started a web server anywhere or hooked to it. There are several different ways to do this, but here's one that works:

var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('')(server);


Note: You must see a server.listen() (or equivalent) somewhere. And, you need to pass the server to so it can hook into it.

You can also do this where you don't directly need to load the http module yourself:

var express = require('express');
var app = express();
var server = app.listen(80);
var io = require('')(server); documentation for several different options here.