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!
});
Related
I am trying to make a front end using node js and angular.
For the backend, I am trying to access the couchdb data in the code as follows:
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const NodeCouchdb = require('node-couchdb');
const couch = new NodeCouchdb({
auth:{
user: 'admin'
password: '**' ///hidden for security
}
});
couch.listDatabases().then(function(dbs){
console.log(dbs);
});
const app = express();
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use (bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function(req,res){
res.render('index');
});
app.listen(3000, function(){
console.log('Server is started on Port 3000');
})
My couchdb host is something like this
*http://admin#***#172.26.132.189:5984*
The above code is just trying to list the databases in my couch db server. But when I run the code it gives me the following error:
{error:unauthorised: name or password is incorrect}
I tried defining my host also in the above code by adding these lines:
const couchExternal = new NodeCouchDb({
host: 'couchdb.external.service',
protocol: 'https://172.26.132.189',
port: 5984
});
But still gives me the same error.
I am giving the correct credentials.
Can someone please help me with a solution or tell me where I'm going wrong?
you are missing a coma after the user i.e. it should have been like: user:'admin', password.... so on since auth is an object variable. Check out this video for more help:
https://www.youtube.com/watch?v=R6LUMXrAoCE&list=PLx3witYKF_5KjQj8i_OTrYbm8WNcZdccG
I am currently working on a project which is a webpage using angular to dynamically change DOM elements. Within the project is a public folder which contains all HTML, CSS, JavaScript and JSON objects. The project must be distributed so I am using node to run from localhost. This is my server.js code:
var express = require('express');
var app = express();
var path = require('path');
app.get('/', function(req,res){
res.sendFile(path.join(__dirname + '/public'));
});
app.listen(8080, function(){
console.log((new Date()) + " Server is listening on port 8080");
});
When I head to localhost:8080 it just says Cannot GET /. Where am I going wrong?
The correct way to serve static files with express is as follows:
//Look for statics first
app.use(express.static(path.join(__dirname, 'public')));
//Return the index for any other GET request
app.get('/*', function (req, res) {
res.sendFile('index.html', {root: path.join(__dirname, 'public')});
});
Edit: on a side note this may be worthwhile to mention that app.get should be the last route declared in node so if you want API endpoints exposed declare them above (before) the final app.get.
You forgot to point to the actual html file you want to display. If you have a index.html in your public directory, just point tot '/public/index.html' . That works (tested it here).
Followed user Muli's answer and all files are now being served correctly. Code here:
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req,res){
res.sendFile('index.html', {root: path.join(__dirname, './public')})
});
app.listen(8080, function(){
console.log((new Date()) + " Server is listening on port 8080");
});
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
I can set up the chat no problem, but my question is how do I get the chat to work on the default port:80 where my main site is?
First thing that comes to mind is iframing it?
Here is my server.js code, one thing to note I don't really like jade so I am going to convert that into plain HTML. Also for the chat to work it has to be on any port other than :80 so I am not quite sure how to get it to work on that main port, other than iframing?
So my question is obviously what are my options in getting the express server to work on the main port?
// Start server
var express = require("express");
var app = express();
var port = 3700;
// Directory
app.use(express.static(__dirname + '/chat'));
// Socket.io integration
var io = require('socket.io').listen(app.listen(port));
// Render content
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.get("/", function(req, res){
res.render("page");
});
// Recieve msg and send
io.sockets.on('connection', function (socket) {
socket.emit('message', { message: 'welcome to the chat' });
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
You dont realy need node-expres-jade on the port 80 for these aproach.
Node can just run the socket server (alone). Forget express/jade here.
And apache or nginx can handle all de client-side files (html, js , css ...).
add the var socket = io.connect('httt://yoururl:port'); on any html an it should work.
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