1

I have an app like this

/app
    /views
        index.jade
    /controllers
        controllers1.js
        controllers2.js
server.js

Inside server.js I use app.use(express.static(path.join(__dirname, 'app'))); and all requests render index.jade. Inside index.jade I call the controllers with regular <script> tags.

My Problem

I'm uncomfortable with exposing all my scripts to the front-end. e.g.: I don't want people to type www.myurl.com/controllers/controllers1.js and see the script, because I don't want to show all my business logic right away, especially to users who aren't logged in.

Question

Having deactivated express.static(), is it possible through express Middleware to get the contents a certain javascript files and include/send them to index.jade? Ideally I want to control which script files the front-end can receive/see.

There's probably many ways to do this. Should I be using some kind of library for this (maybe Requirejs, idk much about it)?

2
  • Something doesn't make sense here. Controllers are supposed to be very thin glue logic between the view and model. They're supposed to be just one level higher than the dispatcher (express). Why would you need to include the controller in the browser script? Sounds like you have some model or library code in your controller. Commented Feb 26, 2014 at 18:11
  • I just used controllers as an example here, it could be replaced with services, directives etc.. The idea is for example I have a service which talks to an API that needs to be used in both the public and private part of my app, therefore it needs to be exposed to both. However, I have a another service/controller/whatever that I only use in the private part of my app. I don't want it to be exposed if you're not logged in. Commented Feb 26, 2014 at 18:35

2 Answers 2

1

deactivating express.static is not required

use public folder for all the content which you want to expose to users like

app

->controllers

public

->js

->->jQuery.js

->->abc.js

->css

->->bootstrap.css

htmls goes here

and use express.static(__dirname,'public') to expose public folder.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I see, my issue though is I want expose some controllers, but not others, so it doesn't make sense to include the entire public directory. Is it possible to use express.static as a non-global middleware but rather apply use it conditionally or only on certain files/directories depending on the request.
nodejs.org/api/fs.html#fs_fs_readfilesync_filename_options Read the file you want and just send it to user
1

One strategy I've used is to separate the public and private node files. The public libraries implement dual purpose node/browser code. I use a folder structure that looks something like this:

/app
    /lib
        /public
            public_library.js
        private_library.js

Then it's simple to expose the public directory so that you can do both:

var mylib = require('public/public_library.js');

in your node files and

<script src="lib/public/public_library.js"></script>

in your browser facing code.

Put all libraries you want to expose in the public directory. Put all libraries you want to keep private outside of the public directory.

1 Comment

Admittedly this answer is exactly the same as WebServer's but I was waiting for clarification before posting it. Also I'm hoping my wording makes the idea clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.