Skip to main content

Posts

Introduction to Node

Node.js Node.js is an open-source, cross-platform, JavaScript runtime built on Chrome's V8 Javascript engine that allows developers to create all kinds of server-side tools and applications in JavaScript. Node.js was developed by Ryan Dahl in 2009. https://nodejs.org/en/ Javascript everywhere Great performance Asynchronous and event driven Single threaded Node package manager (npm) provides access to thousands of reusable packages. Portable Express Express is an un-opinionated, minimalist web framework for Node.js https://expressjs.com/ Setting up the Node environment Install Node $ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - $ sudo apt-get install -y nodejs $ node -v Install express generator The express application generator tool generates an Express application skeleton. $ npm install express-generator -g (-g flag installs the tool globally) Create skeleton project $ express --ejs --git hellonode $ cd hellonode $ npm install $...

Layouts in express ejs

Greetings! This is the 5th part of our Node EMS. In my last post I used the same html layout for all the views. But it is still repeating. So one solution for it to use a layout engine. For express ejs we can use express-ejs-layouts . $ git clone https://github.com/slmanju/node-ems.git $ cd node-ems $ npm install $ npm run devstart If you like to check the changes done in this tutorial $ git checkout layout-with-ejs Installation $ npm install express-ejs-layouts --save Usage in app.js we add following, var expressLayouts = require('express-ejs-layouts'); app.use(expressLayouts); app.set('layout', 'layout/main'); Then we create main layout file and use <%-body%> to generate final html. All other html files will have only the body content. out final main.ejs

Creating views with EJS

Greetings! This is the 4th part of Node EMS. Express uses Jade/ Pug as default view template engine. But it is not pure html which is difficult for a non-ui guy. We can change the view engine to EJS using --ejs option when creating the project. Complete code for this tutorial, $ git clone https://github.com/slmanju/node-ems.git $ cd node-ems $ git checkout create-views EJS EJS is a simple templating engine that generate HTML with plain JavaScript. There is no special thing about that since it uses plain HTML and uses JavaScript like syntax to generate dynamic contents. Supports includes, filtters, etc Tags <% 'Scriptlet' tag, for control-flow, no output <%= Outputs the value into the template (HTML escaped) <%- Outputs the unescaped value into the template <%# Comment tag, no execution, no output <%% Outputs a literal '<%' %> Plain ending tag -%> Trim-mode ('newline slurp') tag, trims following newline Create view...

Creating a controller in Node

Greetings! In this 3rd part of the tutorial, we are going to connect our employee schema with the controller. (this is incomplete without views on next tutorial) We are going to use mongoose's built in methods to access database and do our operations. Update employee controller as follow.

Creating models with mongoose

Greetings! This is the 2nd part of our Node employee management system. In this part, we are going to create our model. MongoDB MongoDB is an open source NoSQL database that stores data as documents in JSON like structure. A "collection" of "documents", in a MongoDB database, is analogous to a "table" of "rows" in a relational database. This combination is extremely popular because it helps to use same structure in entire application. (thus creating stacks like MEAN) Mongoose Mongoose is an Object Document Mapper(ODM) for Node. (like Hibernate for Java). It allows us to access MongoDB commands simply and easily. https://www.npmjs.com/package/mongoose Initialize database * You need to have installed Mongo in your computer. go to the terminal and do this, $ mongo > use nodeems; > db.employees.insert({    firstName: 'Manjula',    lastName: 'Jayawardana',    email: 'manjula121@gmail.com',    passwor...