MEAN stack

0. Introduction

MEAN stack
– MongoDB
– Express : Javascript Web framework and add-ons – including Mongoose, an ORM for MongoDB
– Angular : Template
– Node : event-driven, server-side Javascript runtime – powered by the V8 engine

NPM : package manager

X. [nodejs v.s. npm] Around the bush Installation

To fix this error, we have to set the URL

Error: failed to fetch from registry:

use http://registry.npmjs.org/ instead of https://registry.npmjs.org/

$ npm config set registry http://registry.npmjs.org/

NOTE : however, in ubuntu 12.04, nodejs and npm version is conflicted.

npm ERR! error installing express@3.4.4
npm ERR! error rolling back express@3.4.4 Error: UNKNOWN, unknown error '/media/disk-1/testnewfolder/node_modules/express'

npm ERR! Unsupported
npm ERR! Not compatible with your version of node/npm: connect@2.11.0
npm ERR! Required: {"node":">= 0.8.0"}
npm ERR! Actual:   {"npm":"1.1.4","node":"0.6.12"}

1. [nodejs and npm] Real Installation

On ubuntu 12.04, the launchpad repository gives conflict of npm and nodejs ( as in section X. )

Therefore, we need to add PPA following official nodejs documentation

$ curl --silent --location https://deb.nodesource.com/setup_4.x | sudo bash -
$ sudo apt-get install --yes nodejs

2. Express installation

npm usage : command line

Example : install express

 $ npm install express

The folder node_modules/express will be created

npm usage : install all dependencies using package.json

package.json

{
"name" : "MyStaticServer",
"version" : "0.0.1",
"dependencies" : {
"express" : "3.3.x"
}
}

Then run

 $ npm install

TIPS : install and add dependency to package.json

npm install --save socket.io

Run Express

server.js

var express = require('express'),
app = express();

app.use(express.static(__dirname + '/public'));

app.listen(8080);
 $ node server.js

That is the file server that we can access image on, for example http://localhost:8080/my_image.png

Another express example : response send

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
 res.send('<h1>Hello world</h1>');
});

http.listen(3000, function(){
 console.log('listening on *:3000');
});

NOTE : use app.get(‘*’, For page not found

Another : serving HTML

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

ref : https://developers.openshift.com/en/node-js-example-meanstack.html
, http://learn.mean.io/#mean-io-hosting-mean-openshift

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s