I've been trying to find an answer to this, but i can't find anything. Hoping someone here knows a quick fix.
When i format code with alt+shift+f it turns out like this:
app
.get('/', function (req, res) {
res.send('Hello world');
});
I would really like to make it look like this:
app.get('/', function (req, res) {
res.send('Hello world');
});
Does anyone know how to do this?
Thanks
Related
var cookieParser = require('cookie-parser')
var cookieValidator = require('./cookieValidator')
var app = express()
async function validateCookies (req, res, next) {
await cookieValidator(req.cookies)
next()
}
app.use(cookieParser())
app.use(validateCookies)
// error handler
app.use(function (err, req, res, next) {
res.status(400).send(err.message)
})
app.listen(3000)
In the above code both cookieParser() and validateCookies are middlewares but the way of executing them are different. There is a function like () with cookieParser but not with validateCookies. Can someone please explain why? Sorry If I am sounding foolish.
The app.use() function takes a single parameter, which is a function reference for a function that serves as a middleware for your router in Express.
You’ve correctly included the reference to your validateCookies function without parenthesis, as you just want Express to know which function you’d like it to use as middleware, and not to execute it at the time (Express will invoke the function for you when it’s time).
cookie-parser is a bit of an outlier (and I can see from where your confusion stems). The cookieParser() function actually returns a function reference upon successful execution. This design is likely because this particular module allows developers to pass in certain values to change the resulting function’s behavior as a middleware.
I have only one MiddleWare with next called inside, and the request-response cycle is ended even though i did not use the res.send(), how is that?
app.use("/", function (req, res, next) {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
});
I have an Express route for public-sitemap.xml:
app.get("/public-sitemap.xml", function(req, res) {
// do stuff
});
I'd like to support URLs like public-sitemap-1.xml, public-sitemap-2.xml as well.
How can I allow these optional parameters in Express?
If I set the route to /public-sitemap-?:id?.xml that allows public-sitemap-1.xml etc, but it also allows public-sitemap1.xml, which I'd prefer to reject.
Is this what you're looking for?
/public-sitemap(-:id?)?.xml
More info here: https://expressjs.com/en/guide/routing.html
Use this:
app.get("/public-sitemap-:id(\\d+).xml", function(req, res) {
// do stuff
});
I have an route like as '/antalya-ucak-bileti' and 'antalya' word will be dynamically change. How can I handle it in routes?
I'm used NodeJS and ExpressJS
Thanks
This should work:
app.get('/:destination-ucak-bileti', (req, res) => {
console.log('P', req.params.destination);
res.end();
});
code below, when I visit url like this http://localhost/. It can match the first one, but when I visit http://localhost/detail-999, It match the first one again. It can not match the second one.
I want that when I visit http://localhost or http://localhost/list-1-1 can match the first one and when visit http://localhost/detail-999 can match the second one correctly...
I have no idea to fix this problem...
router.get('/|/list-:type-:page', function (req, res) {});
router.get('/detail-:itemId', function (req, res) {});
All you need is to wrap it to brackets like this:
app.get('(/|/list-:type-:page)', function (req, res) {
});
Try this:
router.get('/list/:type/:page', function (req, res, next) {});
router.get('/detail/:itemId', function (req, res, next) {});
It can be problematic designing your routes as you did. If you have params that can not be changed then you should handle the dashes in the route's action method and just do a req.params.list_name.split('-')