express() Flashcards

1
Q

once imported how do you start express?

A

const app = express();

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

how do you add a middleware?

A
app.use((req, res, next) => {
     // ... code here
     next();   // to pass to the next middleware
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how do you build your response i the last middleware?

A

res.send(‘<h1>ha ha</h1>

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

what is the function that express has that simplifies routing? how does it work?

A

express.Router();
creas por ejemplo una function router = express.Router(); then le adicionas los .use , .get or .post que quieras , lile router.get or router.post(‘/’, (req, res, next) => {} y salvas esa funcion en un file aparte, por ejemplo admin.js y luego la exportas . then en tu app.js la importas en app.js y se la puedes poner en app.js como app.use(adminRouter); o sea se la pasas a app.use() como cualquier otra middleware function

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

como haces que un middleware sea solo de get y de post

A

app.use no. usa app.get or app.post

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

como haces el manejo del body parser

A
npm install body-parser --save
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how do you set the statusCode in express?

A

res.status(404).send(‘’);

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

how redirect in expressjs

A

res.redirect(‘/’);

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

how to prepare a 404 message ?

A

ponlo en el last middleware, pasale el 404 de status y un mensaje

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

como se devuelve un file en el response object?

A

res.sendFile(filePath);

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

variable that gives you the filename of the current module that is running?

A

process.mainModule.filename

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

what is a static resource?

A

a file or resource not managed by the routing system but by the file system directly

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

how do you add a static folder to your project?

A

through a new middleware, and passing express.static() and passing a path to standard public folder

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

when a static folder is added to your project, your link in css and any other path that will use this feature must use an absolute or relative path?

A

relative, i.e., from public folder in, e.g.,

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

is the match exact in app.get and app.post or just partial match? and with app.use

A

exact. with use is partial

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