Learn Node JS
Node js http
- What is the command used to import external libraries?The “require” command is used for importing external libraries. For example - “var http=require (“HTTP”).” This will load the HTTP library and the single exported object through the HTTP variable. Now that we have covered some of the important beginner-level Node.js interview questions let us look at some of the intermediate-level Node.js interview questions. var http = require('http');
- How do you create a simple Express.js application?The request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on The response object represents the HTTP response that an Express app sends when it receives an HTTP request
- Why node.js is quickly gaining attention from JAVA programmers?Node.js is quickly gaining attention as it is a loop based server for JavaScript. Node.js gives user the ability to write the JavaScript on the server, which has access to things like HTTP stack, file I/O, TCP and databases.
- What does EventEmitter do?Every object in Node.js capable of emitting events is a member of the EventEmitter class. http module is one such example. All EventEmitter classes can use the eventEmitter.on() function to attach event listeners to the event. Then, as soon as such an event is caught, its listeners are called one by one synchronously. const events = require("events"); const eventEmitter = new events.EventEmitter(); const eventListener = function(){ console.log("event triggered"); } eventEmitter.on("emitted", eventListener); eventEmitter.emit("emitted");
- Why is it a good practice to separate ‘app’ and ‘server’ in Express?By separating app and server in Express, we can separate the API implementation from the network-related configuration. This allows us to carry out API tests without performing network calls. This also guaranteed faster test execution and better code coverage metrics. To achieve this separation, you should declare API and server in separate files. Here we use two files: app.js and server.js. //app.js const express = require("express"); const app = express(); app.use("/", index); app.use("/contact", contact); app.use("/user", user); module.exports = app; //server.js const http = require("http"); const app = require("/app/JobBoard/JobListModule"); app.set('port', process.env.PORT); const http = http.createServer(app);
4iv>