Nilamber Singh
Nilamber Singh

Reputation: 874

How to prevent node js from serving .git folders and all the files under .git

I know that using .htaccess file can restrict files to be served under .git but how do i acheive the same if i'm using node.js server. I use forever to start/stop the servers.

Below is the code.

const bodyParser = require("body-parser");
var request = require("request");
const dotenv = require('dotenv');
const app = express();

//app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname));
app.set('view engine', 'ejs');

dotenv.config();

var environment = process.env.NODE_ENV;
var endpoint = process.env.AUTOMATION_API_ENDPOINT;
var port = process.env.PORT;

console.log('Environment : '+ environment);
console.log('Server Port : '+ port);
console.log('BackEnd server Endpoint : '+ endpoint);

var supplierId = null;
var supplyApiEndpoint = null;

app.get("/", function(req,res,next){
   var env=null;
   var url = endpoint+'/v1/supply/qa/env';
     require('http').get(url, (resp) => {
         resp.setEncoding('utf8');
         resp.on('data', function (response) {
             var body = JSON.parse(response);
             var supplyApiEndpoint = body.endpoint;
             console.log("Endpoint: "+supplyApiEndpoint);
             res.render('index',{env: supplyApiEndpoint});
         });
 });
})```

Upvotes: 1

Views: 320

Answers (1)

valdeci
valdeci

Reputation: 15265

The forever is a tool for ensuring that a given script runs continuously and not a web framework.

To serve your files you will need to use a web framework like express and you will be able to ignore some directories serving only the files you need, for example, to serve your views directory:

app.set('views', path.join(__dirname, '/yourViewDirectory'));

or adding some rules using regex to ignore the files:

app.use([/^\/public\/secure($|\/)/, /(.*)\.js\.map$/, '/public'], express.static(__dirname + '/public'));

You can use other node.js web frameworks to do the same as fastify or koa.

Upvotes: 1

Related Questions