Error while running app.js in node - javascript

I am completely new to node and trying to work with webRTC application.
I have installed node,express and socket.io in my project, However when I try to run app.js I get error. I am unable to debug because I am not experienced enough to spot the error.
The following is the code in app.js
var express = require('express')
, routes = require('./routes')
, http = require('http');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server); // this tells socket.io to use our express server
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
console.log("Express server listening on port 3000");
When I write the command node app.js it shows the following error.
I have setup exprees in my project.
The file structure inside webRTC directory is as follows
Here is the link to webpage that accompanies step by step to setup node,npm express and socket.io inside a peoject . I have followed every step properly but the part where it says to run app.js after downloading socket.io it shows error.
How to install node in ubuntu
Here is the index.js code inside route folder where the error is occuring
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
and here is the image which was previously not visible properly

Related

node js routing with ejs res.render does not work, but res.send does

I am setting up the environment for a node js app.
But the views/ejs files are not being rendered. If i do:
app.get("/", function(req, res){
res.send('Something');
});
This works. But, if I do(having an index,ejs file):
app.get("/", function(req, res){
res.render(index);
});
It does not work, I get "index is not defined" on the cleint side in the web, but no error in the command line.
Here is the app.js:
var express = require("express");
var path = require("path");
var app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.get("/", function(req, res){
res.send('Something');
});
app.get("/", function(req, res){
res.render(index);
});
const port = process.env.PORT || 3000
app.listen(port, function () {
console.log(`Express is running on port ${port}`)
})
IS there something wrong with the app.set parameters, or has something changed? I am following a tutorial which might be out dated, but checking the docs, I do not see an issue.
So, what is wrong here, is there a new way to do the routing with ejs? I know partials are gone now. Does this mean no ejs files at all anymore, and if so, how is it supposed to be done now? By rendering an html file?
Thanks
Well, I'm not a pro of express but here index is not defined because you write it like a variable. Try using something like this
res.render(path.resolve(__dirname + "/views/index"));

Change .ejs to .html

How can I make the following server.js code work for .html files instead on .ejs in Node JS. Thanks!
var express = require('express');
var app = express();
// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 8080;
// set the view engine to ejs
app.set('view engine', 'ejs');
// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname + '/public'));
// set the home page route
app.get('/', function(req, res) {
// ejs render automatically looks in the views folder
res.render('index');
});
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
You should be able to just remove these lines:
// set the view engine to ejs
app.set('view engine', 'ejs');
// set the home page route
app.get('/', function(req, res) {
// ejs render automatically looks in the views folder
res.render('index');
});
and add your .html files to public/.
Testing this with this code
var express = require('express');
var app = express();
// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 8080;
// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname + '/public'));
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
and this directory structure
├── package.json
├── public
│   └── index.html
└── server.js
Works fine for me.

Route an entire folder and its content

I want to protect a folder and its content by redirecting the user back to index.
I've tried this, but it only works partially.
var express = require('express');
var path = require('path');
var http = require('http');
var app = express();
app.set('port', 8080);
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'views')));
app.get('/', function(req, res) {
res.render('index.ejs');
});
app.get('/protected/*', function(req, res, next) {
res.redirect('/');
next();
});
//activating server
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
This routes, for example, "localhost:8080/protected" and "localhost:8080/protected/asdf", but not "localhost:8080/protected/otherPage.html".
In this case asdf is not an actual file, but otherPage.html is. So if the file is there it doesn't redirect, but if it is not then it redirects. Why is this?
Your line dealing with static files app.use(express.static(path.join(__dirname, 'views'))); appears before app.get('/protected') so its being matched first.
If you moved the static handler to later in the code this would work as you require.
However, I would recommend splitting the static items into a separate folder to guard against accidentally revealing any server-side code you might be including in ejs files in the views folder.

Configuring 'simplest' node.js + socket.IO + Express server

Realized after setting up a simple node.js socket.IO server that it isn't sufficient to handle even the simplest webpages containing script tags.
So I investigating express which is a simple web framework for node.js.
After looking thru the express documentation http://expressjs.com/guide.html
I was still confused as to how I simply combine express with socket.IO on a node.js server.
Couple hours of googling later I came across this tutorial
https://www.digitalocean.com/community/articles/how-to-install-express-a-node-js-framework-and-set-up-socket-io-on-a-vps
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http');
var app = express();
var server = app.listen(3000);
var io = require('socket.io').listen(server); // this tells socket.io to use our express server
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
console.log("Express server listening on port 3000");
io.sockets.on('connection', function (socket) {
console.log('A new user connected!');
socket.emit('info', { msg: 'The world is round, there is no up or down.' });
});
My question is, would anyone reading this configure their server differently?
I don't need anything special, no session handling etc, just the ability to serve html pages containing links to external CSS and javascript files.
Remove the first app.configure wrapper but leave it's contents. It is useless in general, but especially if you don't pass an argument to it.
Remove methodOverride and bodyParser as you aren't using them
Thanks for all the replies. Finally have something that works and am posting so someone else may benefit. My first attempt(above) was obviously NOT the simplest solution:)
//npm install express
//npm install socket.io
var express = require('express');
var server = express.createServer();
server
.use( server.router )
.use( express.static(__dirname+'/public') )
.get('/api', function(req, res) {
res.write('API');
});
server=server.listen(3000);
var io = require('socket.io');
var socket = io.listen(server);
socket.on('connection', function (client){
// new client is here!
});

Node.js app, Express and Cloud Foundry

I am using out of the box expressjs app, and I'm using node 0.8.2. Works great locally, but when I push to the Cloud Foundry I get the following error, and I have no idea where to start with debugging. I'm assuming that this is caused by a configuration issue or a dependency issue, but I don't know what to do.
Express
500 TypeError: Object # has no method 'randomBytes'
at Object.uid (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/utils.js:122:17)
at MemoryStore.generate (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/middleware/session.js:203:27)
at generate (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/middleware/session.js:281:13)
at Object.session [as handle] (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/middleware/session.js:290:7)
at next (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.cookieParser [as handle] (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
at next (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:37:5)
at next (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at multipart (/var/vcap/data/dea/apps/dwgapp1-0-690016dc6c7142f385b44b144d3d380e/app/node_modules/express/node_modules/connect/lib/middleware/multipart.js:62:61)
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.VCAP_APP_PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
It seems that this issue was caused by connect, a dependency of express. I had originally created my app using express running on node 0.8.8, and CF was using 0.8.2. Even though I it didn't seem to make a difference locally, when I deployed to Cloud Foundry I was getting this error.
I created an entirely new app, running Node 0.8.2 from the beginning. This seemed to fix the issue.
You can use vmc logs app-name, With the error message displayed.
I encountered the same question with you.
The reason is the default nodejs version is too old for my app,
Just vmc delete app first,
Create new one with newest nodejs, vmc push app-name --runtime node08

Categories