NodeJS - Node.js Mendel Rosenblum CS142 Lecture Notes...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CS142 Lecture Notes - Node.js Node.js Mendel Rosenblum
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS142 Lecture Notes - Node.js Threads versus Events @3?C3AB モ @3/2±3?C3AB²A=193B³z @3>:G モ >@=13AA±3?C3AB²@3?C3AB³z A3<2±3>:G²A=193By @3>:G³z Implementation: Thread switching (i.e. blocking) and scheduler @3/2±3?C3AB²A=193By 4C<1B7=<²@3?C3AB³ ´ >@=13AA±3?C3AB²@3?C3ABy 4C<1B7=< ²@3>:G³ ´ A3<2±3>:G²A=193By @3>:G³z µ³z µ³z Implementation: Event queue processing
Image of page 2
CS142 Lecture Notes - Node.js Event queue Inner loop E67:3 ±[email protected]² ³ 74 ±~3D3<B´C3C3|<=Bµ;>BG±²² ³ 3D3<B´C3C3|>=>±²|1/::±²z Never wait/block in event handler . Example @3/2·3?C3AB±A=193B²z ¯| :/C<16·3/2·3?C3AB±A=193B²z // Returns immediately °| When read finishes: 3D3<B´C3C3|>CA6±@3/2¸=<3µD3<B¹/<2:[email protected]²z
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS142 Lecture Notes - Node.js Node.js Take a JavaScript engine from a browser (Chrome's V8 JavaScript Engine) Get same JavaScript on both browser and server Don't need the DOM on the server Add events and an event queue Everything runs as a call from the event loop Make event interface to all OS operations Wrap all the OS blocking calls (file and socket/network io) Add some data handle support Add a proper module system Each module gets its own scope (not everything in E7<2=E )
Image of page 4
CS142 Lecture Notes - Node.js Example: Node.js reading a file D/@ 4A モ @[email protected]±¢ 4A ¢²z // @[email protected] is a Node module call // fs object wraps OS sync file system calls ³³ OS @3/2±² is synchronous but Node's 4A|@3/2´7:3 is asynchronous 4A|@3/2´7:3±¢ A;/::´7:3 ¢y @3/2µ=<3¶/::0/19²z ³³ Start read 4C<1B7=< @3/2µ=<3¶/::0/19±[email protected]@[email protected] 2/B/·[email protected]² ¸ ³³ Node callback convention: First argument is JavaScript Error object ³³ dataBuffer is a special Node ±uIIHr object 74 ±[email protected]@[email protected]² ¸ 1=<A=:3|:=5±¢ A;/::´7:3 1=<B3<BA ¢y 2/B/·[email protected]|B=¹[email protected]<5±²²z º º
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS142 Lecture Notes - Node.js Node Modules Import using @[email protected]±² System module: @[email protected]±¢4A¢²z ³³ Looks in <=23 ;=2C:3 directories From a file { @[email protected]±¢|³´´´|8A¢²z // Reads specified file From a directory: @[email protected]±¢|³;Gµ=2C:3¢²z // Reads ;Gµ=2C:3³7<23F|8A Module files have a private scope Can declare variables that would be global in the browser Require returns what is assigned to ;=2C:3|3F>[email protected] D/@ <=B¶:=0/:z 4C<1B7=< 4C<1¯±² ·¸ 4C<1B7=< 4C<1°±² ·¸ moGulH.Hxports モ ·4C<1¯{ 4C<1¯y 4C<1°{ 4C<1°¸z
Image of page 6
CS142 Lecture Notes - Node.js Node modules Many standard Node modules File system, process access, networking, timers, devices, crypto, etc. Huge library of modules (npm) Do pretty much anything you want We use: Express - Fast, unopinionated, minimalist web framework (speak HTTP) Mongoose - Elegant mongodb object modeling (speak to the database)
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS142 Lecture Notes - Node.js Node Buffer class Manipulating lots of binary data wasn't a strength of the JavaScript engine Unfortunately that is what web servers do: DBMS Web Server Browser Node add a Buffer class - Optimized for storing and operating on binary data Interface looks an array of bytes (like the OS system calls use)
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern