Search code examples
pythonnode.jspython-requestsaxiosfetch

Anything similar to Python Request.session() but on Node.js


I have a web scraping application written completely in python. The information that I am web scraping is behind a login and I am using Request.session to save login sessions. I am trying to port the code to Node.js and wasn't able to find anything similar to request.session on Node.js. Please let me know if such a thing exists. Thank you.


Solution

  • Your best bet is using axios, which allows for extensive customization of request headers and types of authentication.

    You can add axios-cookiejar for cookie management.

    Simple example:

    const axios = require('axios').default;
    const axiosCookieJarSupport = require('axios-cookiejar-support').default;
    axiosCookieJarSupport(axios);
    
    // you can add an Authorization header to all requests:
    // axios.defaults.headers.common['Authorization'] = 'Bearer token';
    
    
    const cookie = 'lang=en; token=hello';
    
    const body = {message: 'I\'m a message body'};
    
    axios.post(url, body, {
      auth: {
        username: 'username',
        password: 'mypassword',
      },
      headers: {
        'Cookie': cookie,
        'Content-Type': 'application/json',
      },
    })
      .then(res => console.log(res))
      .catch(err => console.error(err));
    
    

    Old Answer:

    I believe the Request object of the default HTTP server does not have what you are looking for.

    But if you opted to use the Express framework, you could find similar functionalities in the express-session package.

    To ilustrate it's capabilities and simplicity, here's a slightly tweaked version of the code example provided in their documentation:

    const express = require('express')
    const parseurl = require('parseurl')
    const session = require('express-session')
    
    const app = express()
    
    app.use(session({
      secret: 'keyboard cat',
      resave: false,
      saveUninitialized: true,
    }));
    
    app.use((req, res, next) => {
      if (!req.session.views) {
        req.session.views = {}
      }
      // get the url pathname
      const pathname = parseurl(req).pathname;
      // count the views
      req.session.views[pathname] = (req.session.views[pathname] || 0) + 1;
      next();
    });
    
    app.get('/foo', (req, res, next) => {
      res.send('you viewed this page ' + req.session.views['/foo'] + ' times');
    });
    
    app.get('/bar', (req, res, next) => {
      res.send('you viewed this page ' + req.session.views['/bar'] + ' times');
    });
    
    app.use((req, res, next) => {
      if (res.headersSent) {
        return next();
      } else {
        console.error('Not found');
        const err = new Error('Not Found');
        err.status = 404;
        return next(err);
      }
    });
    
    app.use((err, req, res, next) => {
      console.error(req.app.get('env') === 'production' ? err.message : err.stack);
      if (res.headersSent) {
        return next(err);
      }
      res.status(err.status || 500);
      res.send(`${err.status} - ${err.message}`);
    });
    
    const http = require('http');
    
    const server = http.createServer(app).listen(3000);
    
    server.on('error', (e) => {
      console.log('>>> Server error');
      console.error(e);
    });
    
    console.log(`>>> Server running on localhost:3000`);