npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

require-node

v2.0.8

Published

A node middleware let that browser js code can require node js code which is still running at node server

Downloads

196

Readme

require-node

A node middleware let that browser js code can require node js code which is still running at node server

Example for how to use require-node in https://github.com/luoshaohua/require-node-example


require-node: In client(eg: Browser), you can REQUIRE and CALL server side javascript which is still running at server(eg: Node.js).

require-node 让您能在前端(比如:浏览器)require后端javascript代码并调用,而这些后端代码在执行时依然在服务器上执行,而非浏览器里。

For example: A javascript module(test.js) in Node server, you can require and call that in Browser, like this:

比如:您在Node服务器有一个模块test.js,您可以在浏览器中如下引用并调用:

In Browser code :

var test = require('path/to/test.js')

var sum = await test.add(1, 2)     // sum = 3
var env = await test.getNodeEnv()  // env is server env value

In Node server code (test.js) :

exports.add = function (a, b) {
    return a + b;
}

exports.getNodeEnv = function () {
    return process.env;
}

Note: also support promiseasync/await and import grammar.

Installion

$ npm install require-node

Use

middleware: function (req, res, next) { ... }

var requireNode = require('require-node')
var middleware = requireNode({ base: "path/to/server" })

You can use this middleware in node HTTP :

require('http').createServer(function (req, res) {
    middleware(req, res, function () {
        //req that require-node not process
    }
})

also, you can use this middleware in node EXPRESS :

var express = require('express')
var app = express()
app.use(middleware)

or, you can use this middleware in node KOA :

var Koa = require('koa')
var app = new Koa()
app.use((ctx, next) => {
  var { req, res, body } = ctx;
  req.body = body;
  return middleware(req, res, next);
})

Config options

1. base: '/path/to/server', ONLY this config is necessary!

Config which back end file or dir can be use in front end. base can be set String/Array/Object.

2. path: '/require-node' (default)

Config which url path to be use sending ajax.

3. isDebug: false (default)

Config require-node output log or not.

4. hook function

Sometimes, for some reason(example: security or login), we will prevent some function calls. For each http request before processing, require-node calls preFetch in Browser and calls preCall in Node Server (if throw error or return promise reject, call will be prevent), after server api function called, postCall will be call in Node Server and postFetch alse be call in Browser.

preFetch: function (options) { return promise or common value; }
preCall: function (options) { return promise or common value; }
postCall: function (apiReturn, options) { return a new apiReturn; }
postFetch: function (apiReturn, options) { return a new apiReturn; }

options's structure: { req, res, moduleName, functionNames, formalParams, actualParams }
Note: In Browser, req is xhr(XMLHttpRequest) object.

5. inject: function(req, res){ return {curUser: req.session && req.session.curUser}; }

Use inject config, you can define Custom Injected Services. For more details, please refer to the next section: Inject Service.

Inject Service

1. Use Default Service

If your back end function want use variable $req$res$session、http $body, you can define back end function like this:

function say(arg1, arg2, $req, arg4, arg5){
    console.log($req) // $req is node http req object
}
exports.say = say

require-node will inject variable req to $req.

2. Use Custom Service

If want define Custom Injected Services, you can config like this(eg: curUser service):

var middleware = require('require-node')({
    base: "path/to/server"
    inject: function(req, res){
        return {
            curUser: req.session && req.session.currentUser, //if you store currentUser in req.session
            otherService: ...
        }
    }
})

In your back end function, you can use curUser like this:

function say(arg1, arg2, curUser, arg4, arg5){
    console.log(curUser)
}
exports.say = say

That's all.


Example for how to use require-node in https://github.com/luoshaohua/require-node-example