-1

I m using this script to start my next js app, i cant share whole script, but i need to some help to know:

what is use of compression ? what is use of helmet ? what is use of next({dev}) ?

const express = require('express');
const next = require('next');
const helmet = require('helmet');
const compression = require('compression');

const nextApp = next({ dev });
const nextRequestHandler = routes.getRequestHandler(nextApp);

nextApp.prepare()
    .then(() => {
        const server = express();
        server.use(compression());
        server.use(helmet.noSniff());
        server.use(helmet.hsts({ preload: true }));
        server.use(helmet.hidePoweredBy());

        const requestFun = (req, res) => {
            const parsedUrl = parse(req.url, true);
            const { pathname } = parsedUrl;
            //res.setHeader('X-XSS-Protection', '1; mode=block');

            if (pathname === 'xyz' || pathname.startsWith('xyz') || pathname.startsWith('xyz' || pathname.startsWith('/_next'))) {
                const filePath = join(__dirname, '../public', pathname);
                nextApp.serveStatic(req, res, filePath);
            } else {
                nextRequestHandler(req, res, req.url);
            }
        }

        

        

        server.listen(port, (err) => {
            if (err) {
                throw err;
            };
            console.log(`Server started on port ${port}`);
        })
    })
    .catch((ex) => {
        console.error(ex.stack);
        process.exit(1);
    });
1
  • Helmet is a library for easier security by setting some HTTP headers. Compression is a compression middleware for Express, and next() likely comes from Express which is a Node server framework that most libraries are based on. Aug 29, 2022 at 7:22

1 Answer 1

0
  1. Compress is a middleware to compress the request bodies if possible and traverse/pass it through to the next middleware. And Compress will never compress the responses. For more details, you can read all and get to know some extended features of the package using the official docs "https://www.npmjs.com/package/compression"
  2. Helmet is an express middleware that helps to send HTTP headers in a more secure way and a helmet is The top-level helmet function is a wrapper around 15 smaller middleware. Please go through it once to get to know that package features in detail "https://www.npmjs.com/package/helmet".
  3. Next is a framework to create node servers specifically for backend applications and next() basically is a method/middleware which requires hostname and port and some optional parameters such as environment to create an instance of next and you can store into a variable "const app = next({ dev, hostname, port })". You can get more detail from the official docs "https://nextjs.org/docs/advanced-features/custom-server".
2
  • Your answer could be improved with few changes in the statement. 1) In the first point, you have mentioned it as Compress is a middleware attempt to compress the request bodies. But text in the link says "The middleware will attempt to compress response bodies for all request ...". So the meaning changes. 2) In the second point,it would be better to include Helmet is Express middleware. Though the question is regarding express, it would help others if "Express" word is emphasised. Please edit to improve the answer.
    – Sriram
    Sep 2, 2022 at 5:27
  • Thanks, @Sriram for the suggestions, and the changes you asked for were obvious and I have updated it already. Sep 2, 2022 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.