Skip to content

Instantly share code, notes, and snippets.

@nikkaroraa
Created June 12, 2017 22:36
Show Gist options
  • Save nikkaroraa/91786ddc19146c252cbf2d5198c24d57 to your computer and use it in GitHub Desktop.
Save nikkaroraa/91786ddc19146c252cbf2d5198c24d57 to your computer and use it in GitHub Desktop.
Code @https://cb.lk/nodeintro
res.send("<html><b>hello</b></html>"); //html can be used with res.send() but not with res.write()
var express = require('express');
var app = express();
app.use('/', express.static(__dirname + '/public/'));
//statically mounting a folder or mounting static folders.
//the folder -> __dirname + '/public' is statically available on '/' route
app.get('/', function(req, res){
res.send('Hello')
});
//when 2 requests are made to the same route, then the one that comes first, gets served.
//app.use('/', express.static(__dirname + '/public/'));
//statically mounting a folder.
app.get('/hello', function(req, res){
res.send('Hello')
});
app.listen(2222, function(){
console.log("Listening");
});
Refer: https://expressjs.com/en/starter/static-files.html
Static directories
The files for your pages themselves are still static. That is, you are not creating them dynamically with server-side code. What happens in the browser doesn't matter in this context... the idea is that you do not need to generate these files on the fly, as their content does not change.
Static files are files that clients download as they are from the server
query and queryString difference
https://stackoverflow.com/questions/29960764/what-does-extended-mean-in-express-4-0
The reason for this message is that body-parser is about to change default value for extended from true to false.
Extended protocol uses qs library to parse x-www-form-urlencoded data. The main advantage of qs is that it uses very powerful serialization/deserialization algorithm, capable of serializing any json-like data structure.
But web-browsers don't normally use this protocol, because x-www-form-urlencoded was designed to serialize flat html forms. Though, it may come in handy if you're going to send rich data structures using ajax.
querystring library` provides basic serialization/deserialization algorithm, the one used by all web-browsers to serialize form data. This basic algorithm is significantly simpler than extended one, but limited to flat data structures.
Both algorithms work exactly the same with flat data.
Now, when you know pros and cons of both algorithms, it's up to you to decide which one suits your application better.
var a =false;
setTimeout(function(){
a = true;
}, 1000)
while(!a){
console.log(a);
}
setTimeout(function(){
a = true;
}, 1000)
uneval(Math.pow()) //written in interpreter. Native code, Already compiled code
EventLoop in JS
EventLoop keeps on executing.
If the engine is processing, then it takes blank code and returns a busy signal.
If it is not processing anything
JS is not asynchronous by itself.
There are methods that make it asynchronous.
- nextTick (available only in node JS) (process.nextTick(cb)) -> next time the event clock (the tick) is free, then call cb();
- setInterval
- setTimeout
If a function has a callback, then it doesn't mean that it is an asynchronous function.
fs.write works with file descriptor
fs.writeFile works with file names.
Error-First functions - Functions which have err as their first argument. function(err, ...){}
Buffer:
Pure JavaScript is Unicode friendly, but it is not so for binary data. While dealing with TCP streams or the file system, it's necessary to handle octet streams. Node provides Buffer class which provides instances to store raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.
buf = new Buffer(26);
for (var i = 0 ; i < 26 ; i++) {
buf[i] = i + 97;
}
console.log( buf.toString('ascii')); // outputs: abcdefghijklmnopqrstuvwxyz
console.log( buf.toString('ascii',0,5)); // outputs: abcde
console.log( buf.toString('utf8',0,5)); // outputs: abcde
console.log( buf.toString(undefined,0,5)); // encoding defaults to 'utf8', outputs abcde
Refer: https://www.tutorialspoint.com/nodejs/nodejs_buffers.htm
Without JS:
Handlebars: Templating Engine
SQLDatabase: CRUD Operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment