# httpStatic and projects (followup and solution)

**URL:** <https://discourse.nodered.org/t/httpstatic-and-projects-followup-and-solution/65596>\
**Category:** General\
**Tags:** node-red-dashboard\
**Created:** [25 July 2022 17:03 UTC](https://discourse.nodered.org/t/httpstatic-and-projects-followup-and-solution/65596 "2022-07-25T17:03:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![he\_nodered](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@he\_nodered](https://discourse.nodered.org/u/he_nodered)\
**Post date:** [25 July 2022 17:03 UTC](https://discourse.nodered.org/t/httpstatic-and-projects-followup-and-solution/65596/1 "2022-07-25T17:03:41Z")

</div>

As now V3.0 is out, I've thought, that I look into this again - the main idea was to add static html/js/png-files to the project and serve these through the node-red-dashboard (see my original post here: [httpStatic and projects - General - Node-RED Forum (nodered.org)](https://discourse.nodered.org/t/httpstatic-and-projects/62756).

With the information from ([Project directory path - General - Node-RED Forum (nodered.org)](https://discourse.nodered.org/t/project-directory-path/14424)), I've modified the [node-red-contrib-static (node)](https://flows.nodered.org/node/node-red-contrib-static) to automatically prefix the node with the project folder - this allows me to make a project stored in git fully self contained, including all needed resources (and without creating additionally npm packages).

Here is the modified code for static.js, everything else here: [node-red-contrib-static (node)](https://flows.nodered.org/node/node-red-contrib-static)

```auto
// Serve static files through node-red with a project specific root
// folder (if projects are active)
//
// To use, add the following nodes:
//
// httpIn --> static node (this one) --> httpResponse
//
// Setup the nodes as follows (typically to serve static files, e.g. for a
// template node or other project specific resources like png, js, ...):
// - httpIn: Method = GET, URL = /static/*
// - static: Folder = /home 
// - httpResponse: (no settings needed)
//
// This setup will serve files stored in <project>/home to be accessed through
// the http://<node-red-url>/static endpoint. 
// 
// See the following for the original static node and informaiton 
// about how to get the git project folder:
// - https://flows.nodered.org/node/node-red-contrib-static
// - https://discourse.nodered.org/t/project-directory-path/14424/8
//
const path = require('path')
const serveStatic = require('serve-static')

let StaticInitFunc = function (RED) {
  function StaticNode(n) {
    RED.nodes.createNode(this, n)

    const node = this
    const folder = n.folder
    
    let baseFolder = ''
    const settings = RED.settings
    const userDir = settings.userDir
    const project = settings.get('projects')
    // Check, if the project mode (GIT) is active
    if (project && project.activeProject) {
      // yes, build the "new" httpStatic path - userdir + project + folder
      baseFolder = path.join(userDir, 'projects', project.activeProject, folder)

      // directly setting the httpStatic folder does not work: 
      // settings.set(httpStatic, newStatic)
    } 
    else {
      // no, use userdir + folder
      baseFolder = path.join(userDir, folder)
    }
    console.debug('node-red-contrib-static: Serving: ', baseFolder)

    const serve = serveStatic(baseFolder, { index: ["index.html", "index.htm"] })

    node.on("input", function (msg) {
      RED.log.info('My custom node received a new message!')
      const req = msg.req
      const res = msg.res._res
      req.pathname = req.path = req.url = `/${Object.values(req.params)[0]}`
      serve(req, res, function (t) {
        node.send(msg)
      });
    });
  }
  RED.nodes.registerType("static", StaticNode)
};

module.exports = StaticInitFunc

```

I hope this helps anybody looking for a similar solution!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 September 2022 17:04 UTC](https://discourse.nodered.org/t/httpstatic-and-projects-followup-and-solution/65596/2 "2022-09-23T17:04:25Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
