Error: Cannot GET / - node.js

I am working in c9.io ide environment, I have written below code in server.js file
var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');
var express = require('express');
var app = express();
var router = express();
var server = http.createServer(router);
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
console.log("Server listening at", addr.address + ":" + addr.port);
});
app.use(express.static(__dirname + '/client'));
// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
res.render('index.html');
});
app.get('/about', function (req, res) {
res.send('about');
});
After running node server.js in terminal the message given as
Your code is running at https://nodejs2-mujaffar.c9.io.
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!
Server listening at 0.0.0.0:8080
But after accessing https://nodejs2-mujaffar.c9.io/ url -- It is not rendering view only displaying message Error: Cannot GET /
What I am doing wrong?
Please help.

You seem to have created two instances of express which may be your problem.
Try changing:
var express = require('express');
var app = express();
var router = express();
var server = http.createServer(router);
to:
var express = require('express');
var app = express();
var server = http.createServer(app);
At the minute, your express app variable is not bound to your http server. You have instead bounded an unused instance called router. But then you have registered your routes to the app variable.

Related

How to create multiple socket server instance on the same server

I would like to know if it's possible to create multiple socket servers on differents url routes on only one nodejs server.
I'm trying to have two differents server instance running on differents url
like mysite/instanceA and mysite/instanceB
When i tried to do that with port and not url. The application start well and on the port 3000 and 3001, i got two distincts application. It's great.
let express = require('express');
let http = require('http');
let socket = require('socket.io');
let workspace = require('./src/workspace');
class Server {
constructor(port){
this.httpServer = http.Server(app);
this.io = socket(this.httpServer);
workspace.newWorkspace(this.io , app);
this.httpServer.listen(port, function(){
console.log('App started on : ' + port);
});
}
}
let app = express();
app.use(express.static("public"));
new Server(3000);
new Server(3001);
But, i want do that with url and not port, so i tried something like that. I know why is not worked but i didn't know how to realize my aim.
Example:
let express = require('express');
let http = require('http');
let socket = require('socket.io');
let workspace = require('./src/workspace');
const port = process.env.PORT || 3000 ;
class Server {
constructor(){
this.httpServer = http.Server(app);
this.io = socket(this.httpServer);
workspace.newWorkspace(this.io , app);
this.httpServer.listen(port, function(){
console.log('App started on : ' + port);
});
}
}
let app = express();
app.use(express.static("public"));
app.get("/", function(req, res){
new Server();
});
app.get("/a", function(req, res){
new Server();
});

node/express application started but not listing on any port

I have below application successfully started but it doesn't listen on port 8080.
var app = require('../app');
var debug = require('debug')('shanadminpanel:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '8080');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
When I enter url localhost:8080, nothing happened.
You have use http
So you can create server by any of this .
Two way you can create server
by http module
var http = require("http");
var server = http.createServer(function(request, response) {
console.log("server created")
}).listen(80);
And by express you can do this.
by express
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
})
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})

Node and Express Routing - 404 error

I'm getting back 404 error using nodemon and not quite sure where the problem is. Any tips / resources appreciated!
project root
$ curl http://127.0.0.1:3000/v1/protected
Cannot GET /v1/protected
nodemon: "GET /v1/protected/ HTTP/1.1" 404 26 "-" "curl/7.49.1"
index.js
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
var router = require('./services/router');
mongoose.connect('mongodb://localhost:introToBackend/introToBackend');
app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(express('/v1', router));
const PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '127.0.0.1';
console.log('Listening on', HOST, PORT);
app.listen(PORT, HOST);
services/router.js
var router = require('express').Router();
function protectedRoute(req, res, next) {
res.send("The secret!");
}
router.route('/protected')
.get(protectedRoute);
module.exports = router;
Try writing
express.use('/v1',yourRouter);
You do not need the express inside app.use

Package express-ws doesn't let clients connect

I have an express router defined for express-ws npm package. The router looks is exported like:
//ws.js
var express = require('express');
var router = express.Router();
var connections = {};
router.ws('/connect/', function (ws, req) {
// console.log(ws);
ws.on('close', function(x, y, z) {
console.log(a, b, c);
});
});
module.exports = router;
The (not all) statements in the express app are (and in order):
//app.js
var app = express();
var expressWs = require('express-ws')(app);
var index = require('./routes/index');
var ws = require('./routes/ws');
...
...
app.use('/ws', ws);
app.use('/*', index);
The server has statements like:
//bin/www - Generated by express generator
var app = require('../app');
var debug = require('debug')('server:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
After I run it, I am not able to connect to /ws route over websocket. Am I missing something here? Is there a better way to create websocket running on Express 4?
This is the error I see at the client! What am I doing wrong?
changes to app.js
var app = express();
var server = require('http').Server(app);
var expressWs = require('express-ws')(app,server);
...
...
//module.exports = app;
module.exports = {app: app,server: server};
changes to bin/www
//var app = require('../app');
var app = require('../app').app;
...
...
//var server = http.createServer(app);
var server = require('../app').server;
I know this is old but here's the issue. HTTP server isn't required.
//app.js
const express = require('express');
const { app } = require('express-ws')(express());
const index = require('./routes/index');
const ws = require('./routes/ws');
...
app.use('/ws', ws);
app.use('/*', index);
...
app.listen(8080, () => console.log('Listening on port: 8080');

Multiple node apps in sub folders

I want to create a site with this structure:
--mysite.com
--mainserver.js
-----mysite.com/project1
-----server.js
-----mysite.com/project2
-----server.js
In each project folder I want to run a separate node application. Im trying to do this using vhost module.
In my mainserver.js I have this to test:
var express = require("express");
var app = express();
var router = express.Router();
var vhost = require('vhost');
var app2 = express();
app2.get('/', function(req, res) {
res.send("echo");
});
app.use(vhost('localhost/project1', app2));
app.get('/', function(req, res) {
res.send("hi");
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
//console.log("Listening on " + port);
});
When navigating to localhost:8000 I see the "hi". But when I navigate to localhost:8000/project1 I get Cannot GET /test...
Please help!
I believe this is what you are trying to achieve. (Without use of vhost)
project1/index.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
return res.send('project 1');
});
// Other routes specific to project 1 here
module.exports = router;
Project2 could be set up similarly.
server.js
var express = require('express');
var app = express();
app.use('/project1', require('./project1'));
app.use('/project2', require('./project2'));
var port = Number(process.env.PORT || 5000);
app.listen(port);

Resources