Error with Simple Express Server and Gulp - node.js

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}));

Related

How to update express/lib/application.js to match different app.js file?

MEAN noob here. I was following this tutorial, but npm start gives me the following error:
Error: Cannot find module './view'...
at Object.<anonymous> (C:\root\node_modules\express\lib\application.js:22:12)
This is what application.js says at that location:
var query = require('./middleware/query');
var debug = require('debug')('express:application');
var View = require('./view'); /////// line 22
I have a lib/middleware but not a lib/view, nor lib/util...
My theory is perhaps I screwed the express installation up: It's possible I didn't have the correct app.js in place when I installed it. Does app.js affect express's installation?
Is there a way to rebuild express or express/lib without uninstalling & reinstalling everything?
Would this even fix my problem?

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

node app.js localhost not working

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.

Grunt and express.js server

Currenty I'm using grunt with karma and jasmine to run my tests etc. for my Angular app.
I want to connect this app to a mongo database and was wondering what the best way to do this is. Should I keep using grunt and just connect to a database and use it all the way, or should I use an Express server as my main server connected to the database and run the tests with grunt?
Initially I want to publish this project to heroku, and I know you can do this by just adding a static server.js (wich I do not currently have) like this.
var express = require('express');
var port = process.env.PORT || 3000;
var app = express();
app.use(express.static(__dirname + ‘/public’));
app.listen(port);
and modify the gruntfile.js with this:
tasks
grunt.registerTask('heroku',
['compass:dist', 'autoprefixer', 'imagemin']);
What is the best way to do this?
I see, I feel you have slight misconception of what grunt is. Grunt is a task runner. It will run other commands when for each task. Say for example if you can compile css or minifiy js or combine images before starting server you can do it with grunt. But that does not mean grunt can do all those by itself. It will be using other libraries for those.
If you are using grunt to do testing you internally using jasmine or karma js or something else. Same when you say grunt serve you use express internally start server. So grunt does not connect to mongodb. It is express which connects to mongodb. You can write grunt tasks which will start mongodb and start express server but grunt can not do either by its own.
Should you use grunt? Yes of course.

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');
});

Resources