GithubHelp home page GithubHelp logo

first-app's Introduction

INFORMATION ABOUT NODE AND A LITTLE EXPLANATION ON NODE MODULES

the first file define is called the main module and any other modules are sub modules

this is a modul, and a module allow us to separate different code from each other and they do not overright themselves and a module can be reusable A module has json objes which are called properties and it has the following module { id: '.', exports: {}, parent: null, filename: '/Users/.....', loaded: false, paths: [ '/Users/../.../../..first-app/node_modules', '/Users/../.../../../node_modules', '/Users/../.../../../node_modules', '/Users/../.../../../node_modules', '/node_modules'

]

}

We are adding the method log into exports and logging the function into the log and exporting the module to load a module, we use the require function and it takes in one argument of the type of module we want to load If the module is in the same folder meaning current folder, we use require('./logger') If it is in a sub folder we use require('./subfolder/logger') If the module is in a parent foldr we can use require('../logger'), here we are using the relative path of the target module The requre function returns object that is targetted from the require function i.e var logger = require('./logger')

HTTP Module: this is used for creating networking application

  1. We load http using const http = require('http')
  2. we then call http using the code below, with the code below, we can create a web server and store it in a server object const server = createServer()
  3. const server = http.createServer(); we then handle the event by using on() method Which is event listerner or handler, but the code below is very low level, so i am going to comment is out and make use of req and res server.on('connection', (socket) => { console.log('New connection ....'); });

we then call the server to listen to a predefine port server.listen(5000); we then log the server console.log('Listening on port 5000...');

back to 2. instead of using server.on() event handle because the code is a low level code, we then use a call back function and pass an argument (req, res) instead of working with socket as we did on 3., we can work with the actual request or response object using the if statement const server = http.createServer((req, res) =>{ if(req.url === '/'){ res.write('Hello world'); res.end(); } }); we will see the result of Hello world in our browser ....... 4. if we want to build backend service for our web app or mobile app, we need to handle various router. for example, we can have another statement which will return list of courses from the database [array of object in json] const server = http.createServer((req, res) =>{ if(req.url === '/'){ res.write('Hello world'); res.end(); } if (req.url === '/api/courses'){ res.write(JSON.stringify([1,2,3])); res.end(); } });

going forward, we will not going to use this http module to build a backend service for our application, the reason is because the more we add more route, the code become complex, because we add all of then in a linear way in the call back function, so we use a frame work called express which gives our application a clean structure to add route.

first-app's People

Contributors

phantware avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.