Web Computing Exam Q&A
Q: What is Node.js? Advantages of Node.js.
A: Node.js is an open-source, cross-platform runtime environment that allows you to run JavaScript
on the server side. It uses the V8 JavaScript engine, developed by Google, to convert JavaScript
code into machine code.
Advantages:
1. Asynchronous and Event-Driven: Non-blocking I/O for handling multiple requests simultaneously.
2. Single Programming Language: Uses JavaScript, allowing developers to use a single language
for both client and server.
3. High Performance: Powered by the V8 engine, offering fast code execution.
4. Scalable: Excellent for handling real-time applications like chat and streaming.
5. Large Ecosystem: Rich library of open-source packages available through npm.
Q: Demonstrate working of Node.js by creating a simple server to display a 'welcome'
message.
A: Here is a simple Node.js server that displays 'Welcome' when accessed:
```javascript
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Welcome');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
```
This code creates a server that listens on port 3000. When you access http://localhost:3000, it
responds with 'Welcome'.
Q: Explain callback in Node.js with example.
A: A callback is a function passed as an argument to another function. It is executed after the
completion of the given task.
Example:
```javascript
function greet(name, callback) {
console.log('Hello ' + name);
callback();
function sayGoodbye() {
console.log('Goodbye!');
greet('Alice', sayGoodbye);
```
In this example, sayGoodbye is passed as a callback to greet and is called after the greeting
message.
Q: What is Express.js? What is it used for?
A: Express.js is a web application framework for Node.js. It is used for building web applications and
APIs, handling routing, and simplifying HTTP request and response handling. Express provides a
robust set of features for building single-page, multi-page, and hybrid web applications.
Q: Advantages of Express?
A: 1. Minimal and Flexible: Provides a thin layer of fundamental web application features without
obscuring Node.js features.
2. Efficient Routing: Built-in router allows for clean and organized code.
3. Middleware Support: Middleware functions for handling requests, responses, and other tasks.
4. Easy Integration: Works well with various databases and third-party services.
5. Active Community: Well-maintained with an active community and extensive documentation.
Q: Explain cookies concept in Express with example.
A: Cookies are small pieces of data stored on the client browser, typically used for session
management.
Example with cookie-parser:
```javascript
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
app.use(cookieParser());
app.get('/set-cookie', (req, res) => {
res.cookie('user', 'Alice');
res.send('Cookie has been set!');
});
app.get('/get-cookie', (req, res) => {
const user = req.cookies.user;
res.send(user ? `Hello, ${user}` : 'No cookie found');
});
app.listen(3000);
```
This code sets a 'user' cookie when /set-cookie is accessed, and retrieves it on /get-cookie.