User Tools

Site Tools


node.js

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
node.js [2013/12/17 21:32] – created skipidarnode.js [2023/08/30 11:25] (current) skipidar
Line 1: Line 1:
 +===== Node.js =====
  
 +==== Installation ====
 +
 +== Node.js Server ==
 +At most follow this tutorial: https://devcenter.heroku.com/articles/node-websockets
 +
 +  - BUGS: **Foreman** is not found and **Bad file descriptor** error. Solutions: \\ http://jasonwatmore.com/post/2013/11/20/Getting-Foreman-working-on-Windows-7-after-installing-the-Heroku-Toolbelt.aspx
 +
 +
 +== Hot Code-replacement ==
 +When the server-code is changes, then the node.js app should be restarted. \\
 +To achieve hot-code-replacement the node.js app should be run differently: by using the tool **nodemon**.
 +
 +To install nodemon on server do
 +<code>sudo npm install -g nodemon</code>
 +
 +It then should be used as following to start a node.js application named "server.js"
 +<code>nodemon server.js</code>
 +
 +
 +
 +==== Heroku ====
 +Heroku is the hosting, which is able to host node.js apps too!
 +
 +The howto about deploying the app is here: https://devcenter.heroku.com/articles/getting-started-with-nodejs
 +
 +=== Heroku Addons ===
 +
 +Addons have to be explicetely enabled on Heroku. Possible addons are
 +  * WebSockets
 +  * MongoDB
 +
 +
 +== Enabling Websockets ==
 +How to do that is described here: https://devcenter.heroku.com/articles/node-websockets
 +
 +== Enabling MongoDb ==
 +MongoDB is a NoSQL DB.
 +To use a free Plan from [[https://www.mongohq.com/home|mongohq.com]] 
 + - create an account  
 + - add your data to the url <code>mongodb://<user>:<password>@troup.mongohq.com:10078/buzznodejs</code>
 + - use the library mongoskin as stated [[http://dllhell.net/2011/11/08/on-getting-started-with-node-js-mongodb-and-heroku-on-windows/|here]] <code>npm install mongoskin</code>
 + - Here is a short mongoskin syntax describtion: http://www.hacksparrow.com/mongoskin-tutorial-with-examples.html <code> 
 +// require mongoskin - the mongoDB library
 +var db = require('mongoskin').db('mongodb://<user>:<password>@troup.mongohq.com:10078/buzznodejs');
 +
 +//insert some data
 +db.collection('bands').insert({ name: "guns n' roses", members: ['axl rose', 'slash', 'izzy stradlin', 'matt sorum', 'duff mckagan'], year: 1986 }, function (err, result) {
 +    if (err) throw err;
 +    if (result) console.log('added!');
 +});
 +
 +// retrieve the collection named 'bands'
 +db.collection('bands').find().toArray(function (err, result) {
 +    if (err) throw err;
 +    console.log(result);
 +});
 +</code>
 +
 +
 +
 +===== npx =====
 +
 +https://www.freecodecamp.org/news/npm-vs-npx-whats-the-difference/
 +
 +
 +Sometimes you might want to take a look at a specific package and try out some commands. But you cannot do that without installing the dependencies in your local node_modules folder.
 +
 +That’s where npx comes in.
 +
 +<code>
 +
 +npx @pactflow/swagger-mock-validator@latest <path to oas> <path to pact file>
 +
 +</code>