Showing posts with label nodejs. Show all posts
Showing posts with label nodejs. Show all posts

Friday, May 12, 2017

express: request entity too large

When post with large text, you may get the error "request entity too large", here is the code to fix that.


Thursday, March 30, 2017

passportjs, nodejs auth

The document of passportjs is not good. I followed the document and it did not make my code work.
After reading a couple of tutorial online, I eventually made it work.
I abstracted all passport related setting in one file, auth.js.

Here is the change in the server.js, basically it is adding authentication to the URL you want to protect.

Here are some helpful online tutorial
https://www.danielgynn.com/node-auth-part2/
https://blog.risingstack.com/node-hero-node-js-authentication-passport-js/
http://passportjs.org/docs/facebook
https://github.com/jaredhanson/passport-facebook#re-authentication


Wednesday, March 22, 2017

My 1st MEAN project

MEAN is A MEN
A is angular which is doing the client work.
MEN is server side implemented by Nodejs, Express and MongoDB (Mongoose). Basically use these 3 technologies to build a restful service.

To create a Nodejs project, use "npm init", then start with Express, integrate with Mongoose to access MongoDB. It is quite straight forward.

Here is a good tutorial about Nodejs and Mongoose
https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications

This youtube video teaches you how to build MEAN project step by step. It is awesome.
https://www.youtube.com/watch?v=PFP0oXNNveg&t=2226s
I basically followed the steps in this video. The only difference is that I use Angular 2 CLi to create the client angular 2 project. In the nodejs directory, I ran “ng new client”, which will generate a Angular 2 client project under the client folder.
About how to use Angular 2 Cli, here are the helpful tutorial.
https://scotch.io/tutorials/mean-app-with-angular-2-and-the-angular-cli
https://www.sitepoint.com/angular-2-tutorial/
https://www.sitepoint.com/ultimate-angular-cli-reference/
https://github.com/angular/angular-cli/wiki/build

It seems we shall start two web servers, one is running angular 2. The other one is running nodejs restful service. This may bring in AJAX cross domain issue.

The best way is to run only one web server which can have both angular 2  and restful service. I run "node server.js" which can bring up the restful service. Angular2 need build from typescript to javascript. And the default build directory of Angular 2 Cli is client/dist. We have to let the express server know that.
Here is my server.js

When developing, it is convenient to let it auto-build when files are changed. Run this command in the client folder can implement auto-build.
Then run this command to restart nodejs server, when any change is detected

https://www.npmjs.com/package/nodedev
nodedev allows developers to debug node js code in chrome. Open http://127.0.0.1:7001/debug?port=7000, then I can setup breakpoints and view the variable value. https://bitbucket.org/bonbonniere/mywork/src/7efaf965a5dd0699c5002c512a1f5d1bced2de5f/nodejs/myTaskList/?at=master

Here are some other good tutorials
https://www.youtube.com/watch?v=uONz0lEWft0
https://www.youtube.com/watch?v=_-CD_5YhJTA

Monday, March 6, 2017

nodejs server reload and debug

Nodedev is the wrapper for nodemon and node-inspector.
Nodemon is the tool to reload the server when source files are changed.
Node-inspector allows you debug through chrome for nodejs files.

https://www.npmjs.com/package/nodedev

For example, to run index.js
nodedev index.js

then you can open chrome and visit http://127.0.0.1:7001/debug?port=7000 to start debugging

Friday, March 3, 2017

Mongoose findByIdAndUpdate does not return updated object

Here is the code

You will find the product is the one before update. But usually we need return the one after update. To do that, we need add option {new:true}
http://stackoverflow.com/questions/30419575/mongoose-findbyidandupdate-not-returning-correct-model

Mongoose close connection

Once the connection is created by mongoose, it will create a pool of connections. The pool is maintained by the driver so that the connections can be re-used. The best practice is to create a new connection only once for the whole application.

In MongoDB console, use this command to check the connections opened
The result is
{ "current" : 2, "available" : 999998, "totalCreated" : 14 }
One connection is by the mongoDB console, the other one is nodejs.

The interesting thing is that the number is not increased when I use JMeter to generate 10 concurrent requests.

We shall close the connection before exiting the nodejs program.

After the nodejs program exists, check the connection again and now it is
{ "current" : 1, "available" : 999998, "totalCreated" : 14 }

Here are the helpful articles.
http://stackoverflow.com/questions/23244843/how-mongodb-connection-works-on-concurrent-requests-in-nodejs-express-server

https://gist.github.com/pasupulaphani/9463004#file-mongoose_connet-js

find which command connect to mongodb
http://stackoverflow.com/questions/8975531/check-the-current-number-of-connections-to-mongodb
in windows, use this command to get pid
then use this command to find the process, replace 6888 with the pid found in the previous command

Thursday, March 2, 2017

nodejs mongoose

found a good tutorial about mongoose

https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications

In Mongoose, a sort can be done in any of the following ways:
Post.find({}).sort('test').exec(function(err, docs) { ... });
Post.find({}).sort({test: 1}).exec(function(err, docs) { ... });
Post.find({}, null, {sort: {date: 1}}, function(err, docs) { ... });
Post.find({}, null, {sort: [['date', -1]]}, function(err, docs) { ... });
http://stackoverflow.com/questions/4299991/how-to-sort-in-mongoose


process.env.POST

In Nodejs, you can see the code like this
the port is either process.env.PORT or 3000.
The "process.evn.PORT" can be passed in by command line argument. For example:
now the port is 4444.

Sometimes the setting may be env specific, e.g. database URL. Here is one solution.
https://gist.github.com/wilsonbalderrama/0ad2b64b5ab6287c7318

config.js

Thursday, May 19, 2016

installing ionic on windows complains no c++ compiler

I tried to install ionic on my windows desktop. I got the error

MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005

I cannot find Visual Studio 2005. The version I can find is 2015. I try to install it and it need about 10G space which is a little bit ridiculous.

Eventually I found this solution online and it worked for me

https://github.com/nodejs/node-gyp/issues/802