简体   繁体   中英

Sending only specified javascript files to the front-end

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. eg: 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)?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM