Express.js Flashcards

1
Q

What is Express.js?

A

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the core features in Express.js?

A
  • Middleware: A series of functions that execute in order when a request is made to the server.
  • Routing: A mechanism to define URL routes and their handling functions.
  • Templating: Integrates with various template engines like Pug, EJS, etc., to dynamically generate HTML.
  • Static Files: Serves static files such as images, CSS, and JavaScript.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create a simple server using Express.js?

A
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is middleware in Express.js?

A

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. They can perform various tasks such as executing code, modifying request and response objects, ending the request-response cycle, and calling the next middleware function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you serve static files in Express.js?

A

```javascript
const express = require(‘express’);
const app = express();

app.use(express.static(‘public’));

app.listen(3000, () => {
console.log(‘Server is running on port 3000’);
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you handle different HTTP methods in Express.js?

A

Express.js provides methods for all HTTP verbs, such as get, post, put, delete, patch.
~~~
app.get(‘/’, (req, res) => {
res.send(‘GET request to the homepage’);
});

app.post(‘/’, (req, res) => {
res.send(‘POST request to the homepage’);
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of next in Express.js middleware?

A

The next function is used to pass control to the next middleware function. If next() is called without arguments, the next middleware function in the stack is called. If next is called with an argument, it will skip all remaining middleware and route handlers and will call the error-handling middleware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you handle errors in Express.js?

A

Error-handling middleware is defined with four arguments: err, req, res, and next. It should be added after all other app.use() and routes calls.
~~~
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send(‘Something broke!’);
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you create a route in Express.js?

A

Routes are created by associating an HTTP method with a URL path and a callback function.
```javascript
app.get(‘/user’, (req, res) => {
res.send(‘User page’);
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between app.use and app.get in Express.js?

A
  • app.use: This method is used to mount middleware functions at a specific path. The middleware will be executed for every request to the app.
  • app.get: This method is used to define a route handler for GET requests to a specific path.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you handle query parameters in Express.js?

A

```javascript
app.get(‘/search’, (req, res) => {
const searchTerm = req.query.q;
res.send(Search term: ${searchTerm});
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you handle URL parameters in Express.js?

A

URL parameters can be accessed via req.params.
```javascript
app.get(‘/user/:id’, (req, res) => {
const userId = req.params.id;
res.send(User ID: ${userId});
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you parse JSON and URL-encoded data in Express.js?

A

You can use built-in middleware to parse JSON and URL-encoded data.
```javascript
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you set up a template engine in Express.js?

A

You can set up a template engine like Pug or EJS using app.set.
```javascript
app.set(‘view engine’, ‘pug’);
app.get(‘/’, (req, res) => {
res.render(‘index’, { title: ‘Hey’, message: ‘Hello there!’ });
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you use Express.js to build a RESTful API?

A

```javascript
const express = require(‘express’);
const app = express();
app.use(express.json());

let users = [{ id: 1, name: ‘John Doe’ }];

app.get(‘/users’, (req, res) => {
res.json(users);
});

app.post(‘/users’, (req, res) => {
const newUser = req.body;
users.push(newUser);
res.status(201).json(newUser);
});

app.put(‘/users/:id’, (req, res) => {
const userId = parseInt(req.params.id);
const updatedUser = req.body;
users = users.map(user => (user.id === userId ? updatedUser : user));
res.json(updatedUser);
});

app.delete(‘/users/:id’, (req, res) => {
const userId = parseInt(req.params.id);
users = users.filter(user => user.id !== userId);
res.status(204).end();
});

app.listen(3000, () => {
console.log(‘Server is running on port 3000’);
});
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A