• Monday morning: start learning about Express - a web framework focuses on utilizing http (a core module in NodeJs)

Each http request in browser is sent through many steps: switch - router - backbone - get way -

Request (header, body). Header includes core summarized information about the request e.g. status code, Content-Type,....

After reading the header, the server can parse the body

URL has base URL (https://www.techmaster.vn\

****POSTMAN: a Google Chrome app for interacting with APIs, friendly GUI to read requests and responses, help to debug

****CURL: reads website in terminal

****SUPERTEST: test http server for NodeJs, using alongside with Mocha and Chai to run unit tests

  • Monday afternoon: Express handles http request with multiple middlewares

Express receives requests -> read the header -> parse the body => wrap the request with some additional information

When Express sends back response -> it also wraps the response with some additional information

Middleware e.g. body-parser: parse http request body

****HTTPS: secure http with public and private keys. Each https url has its SSL key so when each request comes, it gets the url public key to encrypt the request. The request is only read by the server holding the private key.

NodeJs only handles http -> Use Nginx (a powerful server) to convert https to http and pass http to NodeJs server

**When there are high traffics -> use several Nodejs or Nginx to handle

  • Tuesday morning:

HTTP request or response includes header and body

Header - meta data about the request/response e.g. content type, status code

Body - actual data

****GET vs POST

  • GET just retrieve info, a request from the browser search bar is almost 99% GET request
  • POST is often used in form, sign in, sign up -> get info from DB

****Client-side and server-side programming

****MODULE.EXPORTS vs EXPORTS.

exports.a = function(){}; works - exports add new properties to the object, so typeof returning result will be an

object:{ a: [Function], b: [Function] }

exports = function(){} doesn't work - exports now points to a new function, not the object that module.exports points to anymore. So the {} modules.exports still points to will be returned.

module.exports is what will be returned when require()

So when we only export a constructor function

module.exports = function Something() {

console.log('bla bla');

}

Now typeof returning result is'function'and you can required it and immediately invoke like:

var x = require('./file1.js')();

because you overwrite the returning result to be an function.

  • Tuesday afternoon: Express routing

Routes maps HTTP request which has a verb GET and URIs (/home) to specific code in server

  • Grabbing parameters to routes: the simplest way is to use colon /:userid

app.get("/users/:userid", function(req, res) {

var userId = parseInt(req.params.userid, 10);

// ...

});

  • Use RegExp if there are more constraints about parameters: UUID-matching routes with RegExp

var horribleRegexp = /^([0-9a-f]{8}-[0-9a-f]{4}-

➥ 4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})$/i;

app.get(horribleRegexp, function(req, res) {

var uuid = req.params[0];

// ...

});

app.get("/search", function(req, res) {

// req.query.q == "javascript-themed burrito"

// ...

});

**Notes: ?arg=something -> typeOf(req.query.arg) == string BUT ?arg=something&arg=somethingelse -> typeOf(req.query.arg)==array

  • licROUTER in Express v.4 to separate routes
  • Serve static files

// ...

var publicPath = path.resolve(__dirname, "public");

var userUploadsPath = path.resolve(__dirname, "user_uploads");

// ...

app.use("/public", express.static(publicPath));

app.use("/uploads", express.static(userUploadsPath));

If the same file image.jpg exists in both 'public' and 'user_uploads', it can be grabbed from both /public/image.jpg and /uploads/image.jpg

Without "/public" and "/uploads" parts in app.use(...), /uploads/image.jpg will be ignored after /public/image.jpg is served

  • Wednesday: understand N-queens ES5 algorithm in Rosetta Code and finish my algorithm

Find all solutions -> draw the N-queens chess board

TODO: change n interactively on browsers and create animation

  • Express: use Multer to upload files

  • Thursday morning:

  • Thursday afternoon

****NODEMON: applies changes in node app and updates automatically without stopping server

****TEMPLATES: Nunjucks, Ejs, Pug, Dust, ... -> choose one that's close to HTML

  • SUNDAY: make animation of N queens algorithm -> break the algorithm into nested if-else -> set up manual mode showing each step of the algorithm and auto run mode

results matching ""

    No results matching ""