node app.js localhost not working - node.js

on WINDOWS ...after install express-seed and node.js for the "blog" tutorial, i get the same cmd prompt after typing node app.js.
another time i got body parser and error handling errors
i tried alot of solutions, even had a local host run with another tutorial, but i would like to run from the blog tutorial due to some slight differences of the set-up.
Of course im a newb, and i know theres tons of answers on the forum, but none are correcting my issue...please help.
and everytime i try to post my report on here it errors me saying i have to indent each line 4 spaces. im just losing in general.
Is there a step im missing? all the tut's say just do 'this' and 'this' and i have a local host running so i can make changes to views. any help?
// Module dependencies.
var express = require('express');
var app = express.createServer();
// Configuration
app.configure( function() {
});
// Routes
app.get('/', function(req, res) {
res.send('Hello World');
});
app.listen(3000);

what version of node & express are you running?
From the command line you can check with:
node --version
and
express --version
From your code, it looks like an older version of express (version 3 or less), but I'm betting you didn't specify the version on the npm install, which will give you the latest version (4+). There's a lot of breaking changes between those versions, so you can't run old code with the new framework successfully. My bet is that your blog tutorial hasn't been updated to express 4.x yet.

Related

How to debug app startup with Gulp

I have run into a road block with a new team I am working with that supports a node app. The app is launched via Gulp, and the setup is such that there is a "core" NPM module that defines a bunch of gulp tasks and a "server", and our app simply installs this package and our code is copied in as a "plugin" to the server.
In our gulpfile.js, we have something like:
var gulp = require('gulp');
var workflow = require('base-workflow');
workflow.use({ gulp: gulp });
gulp.task('default'), ['base:default']);
...more stuff
Where base:default is pulled in and a couple of Hapi servers are ultimately started (one as a "web" app, one as the "rest" proxy app to a real Java-based REST services). What I would like to do is setup node-inpector so that I can troubleshoot the startup of the app because I have found that the latest versions of their base packages are not Mac-compatible.
What I have tried is to install gulp-node-inspector with the following changes:
var gulp = require('gulp');
var nodeInspector = require('gulp-node-inspector');
var workflow = require('base-workflow');
workflow.use({ gulp: gulp });
gulp.task('default'), ['base:default']);
gulp.task('debug', ['default'], function() { gulp.src([]).pipe(nodeInspector({debugBrk: true})); });
...more stuff
and also:
var gulp = require('gulp');
var nodeInspector = require('gulp-node-inspector');
var workflow = require('base-workflow');
workflow.use({ gulp: gulp });
gulp.task('default'), ['base:default']);
gulp.task('debug', function() { gulp.src(['default']).pipe(nodeInspector({debugBrk: true})); });
...more stuff
but neither of those works. Part of this is most likely my lack of understanding of Gulp. Does anyone know how I can debug this app?
I spent a fair bit of time googling and trying the various solutions out there; in the end the one that worked for me was the accepted answer found on this page:
How to debug gulpfile.js
This was the only one that allowed me to actually hit my "debugger" command in my gulp task.
I should also note that I had to completely uninstall and reinstall "node-inspector"; there was a version problem and when I was on the verge of solving it I was getting some "cannot find module" error because the version of node-inspector was causing it to point to the wrong folder. Once I uninstalled and reinstalled (via npm) then it worked. In my case I'm on a Windows machine and the command that worked looked like the following:
node-debug C:\myPathWhereGulpfileDotJsExists\node_modules\gulp\bin\gulp.js --gulpfile C:\myPathWhereGulpfileDotJsExists\gulpfile.js myTestTaskContainingDebuggerCommand
Maby this solution help you
node --inspect --debug-brk ./node_modules/gulp fonts
The best way to do this now is to add a debugger; to the place in the file you would like to add a breakpoint to, or set it manually once the debugger has started with setBreakpoint('gulpFile.js', 1)
Then simply
node inspect --inspect-brk $(which gulp) taskName
c
More information about debugging with node here

Error with Simple Express Server and Gulp

I'm trying to run a simple Express web server using a gulp task. I only want a static server that displays the index file. I can easily perform this by running a node module, but again, I want to do this in gulp. I plan on expanding this to allow a LiveReload server to be set up.
I have followed many tutorials on setting up LiveReload but they are failing. I'm assuming it has something to do with the versions being used with respect to when the articles are written. But I was hoping maybe somebody had an idea on how to handle this.
I have created a very small Github repo that allows you to play around with what I'm trying to accomplish: fixit
Gulpfile.js:
var gulp = require('gulp');
var EXPRESS_PORT = 4000;
var EXPRESS_ROOT = __dirname;
gulp.task('express', function () {
var express = require('express');
var app = express();
app.use(express.static(EXPRESS_ROOT));
app.listen(EXPRESS_PORT);
});
*There is an index.html in the same directory as the Gulpfile
And here is the error:
/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/etag/index.js:55
throw new TypeError('argument entity must be string or Buffer')
^
TypeError: argument entity must be string or Buffer
at etag (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/etag/index.js:55:11)
at SendStream.setHeader (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/send/index.js:724:15)
at SendStream.send (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/send/index.js:500:8)
at onstat (/var/www/clients/client1/web14/sendus-admin/node_modules/express/node_modules/send/index.js:585:10)
at Object.oncomplete (fs.js:97:15)
I had the same problem and after a week without Gulp and BrowserSync working (the combination giving me the same error), I resorted to more severe options like reinstalling Node.js. In the end, what worked for me was to use nvm to downgrade to Node.js version 10 (I was using 11 before).
nvm install 0.10
nvm use 0.10
Then just updated and used Gulp:
npm update
gulp
Sure hope that helps you too.
I had the same problem with Express.static since a week. Disabling ETAG for Express.static solves this problem for me untill there is a better fix:
app.use(express.static(path.join(__dirname, '/static'), {etag: false}));

How to set up an Express 4.0 application

I downloaded express today from npm, and to my surprise, it gave me express 4.0.0 instead of express 3.x.x.
I'm trying to configure a basic server with connect logger and body parser, but I'm not having much luck.
Could someone provide me with a boilerplate app.js file using express 4.0?
Thanks!
Got it!
You can get a skeleton app using:
$ npm install -g express-generator
Alternatively, you can swap out the connect logger and body parser for their connect standalone siblings (and it might be useful for learning what each middleware does, rather than relying on the generator to throw together a bunch of dependencies you may not need):
(based on Express 3.x to 4.x Migration guide)
var express = require('express');
var server = express();
...
server.use(require('body-parser')); //previously bodyparser (or rather urlencoded/json)
server.use(require('morgan')()); //previously connect-logger
...
server.listen('3000', function() {
console.log('server started');
});

node.js and express - debugging a route

I am trying to debug my routes while using NodeJS and the Express framework. I installed node-inspector and got it fully working and open in a Chrome tab. Nevertheless, it isn't showing anything when I have this code in my index.js route
exports.index = function(req, res){
var $ = require('jquery');
console.log($('.title').val);
res.render('index', { title: 'Express' });
};
I'm wondering what the reason behind this is. I hope someone can help!
Edit: When I place a console.log in the app.js the debugger isn't showing anything either.
Other info:
I am using Windows
I first run node-inspector
And then in a second terminal I run node --debug-brk app.js
Not sure if this is the problem:
But this -> console.log($('.title').val);
should be this -> console.log($('.title').val());
But honestly I don't think jQuery works well with Node on the backend side. I have used it with express on the client side, but not on the server side.
I have seen this jQuery install for Node:
npm install jquery
https://npmjs.org/package/jquery
I haven't used this install with node so I cannot be certain this works.

Socket.io.js not found on Ubuntu but is on Windows

I've been pulling out my hair trying to figure this problem out. I have a node.js app that works fine in windows. I zipped everything up and put it on my linux box (Ubuntu 12) and installed all of the libraries through npm, and yet I still get a 404 message saying my socket.io.js file cannot be found. I've tried various solutions such as linked to the cdn.socket.io script but that just throws a "require not found" error. My code in my html is as follows:
<script src="/socket.io/socket.io.js"></script>
I've even tried <script src="localhost:4000/socket.io/socket.io.js"></script>
and here is my server side:
var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app);
...
app.listen(4000);
console.log('server started');
This question is similar, however the answer for it is simply is an updated express semantic which shouldn't apply to my code: socket.io.js not found
If my understanding is correct, the script path should work because when socket.io is running, it should direct that request to the right route. I don't really know what else I should look into for a fix, could it be something with the path in Ubuntu?
Any help would be very much appreciated!
Just tested this:
var io = require('socket.io').listen(8000);
Went to localhost:8000 and the 'welcome to socket.io' message showed up so I know it is running....
paths
node_modules: /home/alex/node_modules/socket.io
my node app: /home/alex/documents/project/app.js
I had the same problem and it seemed that the reason was that something went wrong during the installation of socket.io.
Actually the only workaround I found was to make a new directory, copy all your code there and do a fresh install of the Express and socket.io modules:
npm install express socket.io
Maybe I was lucky, but it worked!

Resources