Saturday, 1 April 2017

How to define webpack dynamic context where node.js code try to find a filename with fallback?

I try to webpack a node.js module and stumbling on some code where we try to require a JSON file with a fallback to a file that will exist at all events. The following code is a hard nut for webpack.

var tryRequire = function(filename){
  var result = null;
  if (fs.existsSync(filename)){
    result = require(filename);
  }
  return result;
}

var getI18nStorage = function(culture){
  var cultureParts = culture.toLowerCase().split("-");
  var dir = path.join(__dirname, "../../i18n/address");
  var fn = cultureParts[0] + ".json";
  var filename = path.join(dir, fn);
  var storage = tryRequire(filename);
  if (!storage){
    fn = cultureParts[1] + ".json";
    filename = path.join(dir, fn);
    storage = tryRequire(filename);
    if (!storage){
      fn = "us.json";
      filename = path.join(dir, fn);
      storage = tryRequire(filename);
/*      if (!storage){
        console.log(culture, "nothing found");
        process.exit(1)
      }*/
    }
  }

  return storage;
}

Currently packing var c = getI18nStorage("en-GB"); results in !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; which is understandable :o)

I tried to learn from https://github.com/webpack/webpack/tree/master/examples/require.context but could not transfer the technique to my problem. I think I need to know 2 things:

  • How to manualy add requiered modules to webpack if they cannot be resolved programaticaly?
    • I tried to add context: path.join(__dirname, "../lib/i18n"), to webpack config but this does not include data to bundled output.
  • Is there a way to help webpack to understand this node.js filesystem strategy to try to find a required module?


via Stephan Ahlf

Unable to read URL encode in nodeJS

I am using express framework in nodeJS and trying to send the activation link on email but as i am activating the link the url get encoded and express routes is unable to read and sending to 404 error page.

Url: 

xxx.com/#/user/543fe901b43083207ff0f863c07135b6/xxx.x@gmail.com

On click to URL:

xxx.com/#%2Fuser%2F543fe901b43083207ff0f863c07135b6%2Fxxx.x@gmail.com

Can you Please help me out what I am doing wrong or something I have missed?

Thanks in advance.



via Sikander

access NodeJS process.env in client side

I am solving a challenging ctf problem in which, the flag is in process.env of nodejs server. I can access source code of server ( bcs they public by express.static....) So is there anyway to access process.env of nodejs in client side?



via Nguyen Dong

How to use Firebase Admin SDK with Node.js?

I want to use the Firebase Admin SDK but I have never used Node.js before. I'm using Brackets as my editor and it has a built in web server because I need it for my AngularJS routes. I run my web app through Brackets but I get "require is not defined" in the console.



via Zik

'npm' is not recognized as an internal or external command, operable program or batch file, but it's in my env path

I'm trying to launch on Windows a node.js module that I have written on Linux.

I have set the env path to nodejs, i have installed my dependencies, when i use npm alone it's working. But when I npm start my module, it's telling me this :

my powershell prompt

Here is my package.json's scripts :

enter image description here

Can someone explain to me what's wrong ?

Thanks in advance.



via Devz

Form Submit 404 Not Found Error

Hi I am currently learning NodeJS and the Express framework, but I am having issue with when I try to submit the form it suppose to go the '/users/register' URL but it seems that my app.js is not finding the register.jade file although I have a register.jade under the view folder. Here is some of my codes:

register.jade:

extends layout

block content
    h1 Register
    p Please register using the form below
    ul.errors
        if errors
            each error, i in errors
                li.alert.alert-danger #{error.msg}
    form(method='post', action='/users/register', enctype='multipart/form-data') ...

user.js:

router.post('/users/register', function(req, res, next) { ... });

app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var expressValidator = require('express-validator');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var local = require('passport-local').Strategy;
var bodyParser = require('body-parser');
var multer = require('multer');
var flash = require('connect-flash');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var db = mongoose.connection;

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
...
app.use('/', index);
app.use('/users', users);

app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

Error on my Git Bash:

giterror

Error from the browser:

browsererror

Here is my folder structure:

folderstructure

I am not sure why it errors out 404, thank you in advance!



via Cham

Environment variables readable by Apache/PHP and Systemd/node

On the same server, I have a apache php application and a nodejs application (started via systemd).

For systemd/node, I put the following config:

[Service]
EnvironmentFile=/etc/environment

For apache, I have put the following line in /etc/apache2/envvars

. /etc/environment

My problem is:

  • It works in PHP and standalone node if I put export before each variable, but not in node via systemd
  • It works in node via systemd if I remove the export

Is there a way for me to write these variables in a single place that can be used by Apache/PHP and node started via systemd ?



via abd