Linus Karlsson
Linus Karlsson

Reputation: 35

Cant get it to run, Node.js, express, socket.io and ejs?

I'm trying to learn java-script. i was thinking of making an basic chat app by using Express, stock.io and ejs.

I just cant get it to run.

This is my app.js

var express     = require('express');
var app         = require('express')();
var http        = require('http').Server(app);
var io          = require('socket.io')(http);
var path        = require('path');
var ejs         = require('ejs');
var app         = express();

// view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'viwes'));

app.get('/', function(req, res) {
    res.render('index');
});

io.on('connection', function(socket) {
    console.log('a user connected');
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

My index.ejs

<!DOCTYPE html>
<html>
<head>
    <title>ChatApp</title>

    <link href="/css/style.css" rel="stylesheet" type="text/css" />

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

</head>
<body>
</body>
</html>

And my package.json

  "dependencies": {
    "express":      "4.14.0" ,
    "body-parser":  "1.15.2"  ,
    "socket.io":    "1.6.0",
    "connect":      "3.5.0",
    "ejs":          "2.5.2"
  },
  "engines": {
    "node":         "4.0.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/heroku/node-js-sample"
  },

Right now it just say in the browser:

Cannot GET /

I think its a path problem but what do i know..

Upvotes: 0

Views: 463

Answers (1)

jfriend00
jfriend00

Reputation: 707198

You have created two different app variables, overwriting one with the other and part of your app is using one and part is using the other. Change from this:

var express     = require('express');
var app         = require('express')();
var http        = require('http').Server(app);
var io          = require('socket.io')(http);
var path        = require('path');
var ejs         = require('ejs');
var app         = express();     // this is overwriting your app variable

to this to fix that part of your problem:

var express     = require('express');
var app         = express();
var http        = require('http').Server(app);
var io          = require('socket.io')(http);
var path        = require('path');
var ejs         = require('ejs');

Because you were creating a second app object and overwriting your app variable with it, you were creating routes on an app object that is not connected to your server at all. Thus, your actual server has no routes defined for it and nothing appears to work.

The recommended fix, creates one and only one app object and all parts of your code use that one app object.


In addition, it appears that views is misspelled in:

path.join(__dirname, 'viwes')

Then, in your browser, make sure you're specifying port 3000 since that's the port you are starting your server on.


FYI, this isn't an actual programming error but I personally think it's a bad idea to use http as the variable name for your server. http should be used for the http module (which you may want to use elsewhere in your app sometimes). require('http').Server(app); creates a server object so that's what you should name the variable:

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

Or even better:

var server = require('http').createServer(app);

Then, later on:

server.listen(...);

Upvotes: 2

Related Questions