How to set up an Express 4.0 application - node.js

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

Related

How to get THREE.JS project to work with PHP not just HTML? [duplicate]

Ok I've been playing around with nodejs, expressjs and socket.io to create some applications. But now im coming to the stage where i want to take things a bit further.
I have noticed a few node apps using PHP for twitter auth on their client side. When I attempt to rename my client.html file to client.php and restart the server it throws up a blank page with this
Cannot GET /
How do would serve php files or would i do twitter auto using js?
This is my NodeJS server.js
var http = require('http'),
express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
app.listen(1234);
console.log("server started on port :1234");
As already noted, node.js itself won't interpret php files for you. Available options are:
have nginx in front of node and reverse proxy all request you want to process in node from it
proxy php requests from node to php-capable server
spawn php process on each request in node
make fastcgi link to php-fastcgi using node fastcgi protocol parser
Uh, skip PHP entirely and integrate everyauth into your app.
PHP-EXPRESS should do. Just install the package and follow the docs.
https://www.npmjs.com/package/php-express

json-server 0.8.12 and Express 4.x Routing

I have an existing node-express project running on express using express server and have middlewares and routing on express
var express = require('express');
var app = express();
require('/path/to/express_conf_file')(app);
app.listen(config.port);
I want to use json-server for easy mocking and I've followed this. My current code looks like this:
var express = require('express');
var app = require('json-server');
var middlewares = express();
var server = app.create(); // Returns an Express server
**var router = server.router('db.json');** // Returns an Express router
server.use(middlewares);
server.use(router);
require('./config/express')(middlewares, config);
server.listen(4000);
server.route('db.json') seems to be deprecated in Express 4.x. What needs to be done to use 'db.json' with express 4.x?
I am invoking my application and json-server using concurrenrly using npm start and in package.json I've defined:
"js-server": "json-server --watch db.json --port 4000",
"start": "concurrently \"gulp command\" \"npm run js-server\""
Can somebody please advice as what should be the correct way of using json-server with Express 4.x?
It is solved here. json-server can be mounted at a certain path within express server. No need to start the json-server separately now. Only gulp command can be used to launch express server.
The json server can be hosted separately also and can be accessed through the specified port from different appilication(s).

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.

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

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.

Resources