RahulOnRails
RahulOnRails

Reputation: 6542

NodeJs + Unable to access localStorage inside routes.js

In my NODEjs ( using Express ) application, I want to use Country Code inside routes.js but I am unable to access localstorage inside the routes.js

Please provide some solution.

Upvotes: 0

Views: 415

Answers (1)

Guy
Guy

Reputation: 11305

LocalStorage is only available in browsers on the Window object.

The Window object is not available server side.

MDN

Following your comment, you could implement a route in your express application which takes the IP as part of the body. For this to work you will need body-parser middleware. Example application:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var server;

app.use(bodyParser.json());

app.get('/api/ip', function (req, res) {
  res.send(req.body.ip);
});

server = app.listen(3000);

This would return the posted IP.

Upvotes: 1

Related Questions