express() Flashcards
once imported how do you start express?
const app = express();
how do you add a middleware?
app.use((req, res, next) => { // ... code here next(); // to pass to the next middleware }
how do you build your response i the last middleware?
res.send(‘<h1>ha ha</h1>
what is the function that express has that simplifies routing? how does it work?
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
como haces que un middleware sea solo de get y de post
app.use no. usa app.get or app.post
como haces el manejo del body parser
npm install body-parser --save const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({extended:false}));
how do you set the statusCode in express?
res.status(404).send(‘’);
how redirect in expressjs
res.redirect(‘/’);
how to prepare a 404 message ?
ponlo en el last middleware, pasale el 404 de status y un mensaje
como se devuelve un file en el response object?
res.sendFile(filePath);
variable that gives you the filename of the current module that is running?
process.mainModule.filename
what is a static resource?
a file or resource not managed by the routing system but by the file system directly
how do you add a static folder to your project?
through a new middleware, and passing express.static() and passing a path to standard public folder
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?
relative, i.e., from public folder in, e.g.,
is the match exact in app.get and app.post or just partial match? and with app.use
exact. with use is partial