Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
A node_modeule is basically a lego block of code. Everything in a node application is modular, meaning it is made up of many different smaller parts that work towards the end functionality.
NPM stands for Node Package Manager. It's just a tool that allows us to easily acces the node module ecosystem and add them to our project. Example node_module: Moment.js
Every node app has a package.json , which is the master file that keeps track of all node maodules your project cares about.
to add a package.json to your project, run the commmandnpm init , and accept all defaults.
git add -A
git commit -m "introductory definitions for node"
git push origin master
We used the node module HTTP to build a simple web server, that we can then run and connect to using the web address: http://localhost:3000/ .
To run the server use the command node index.js
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello world!');
}).listen(3000, function(){
console.log('App is listening on port 3000')
});Express API
To add express node module to our application:
npm install --save express
To ensure it was added succesfully, check your package.json for:
"dependencies": {
"express": "4.15.3"
:}This also gives us a ton of code we do not care to track in our Github, in the node_modules/ folder. To ignore this code;
touch .gitignore
add node_module/ to .gitignore
nodemon will watch our files automatically and restart our server so we don't have to do it manually.
nodemon index.js -> to start server
how to install: sudo npm install -g nodemon
When using postman you have to make sure that your names match in on your as well as in postman.
app.post('/fish', function(req, res){
var type = req.body.type;
var length = req.body.length;
var color = req.body.color;
var fish = {type: type, length: length, color: color};
res.json(fish); ```