Should I use express static dirname or use Node.js as a remote server?

node.js express

102 观看

1回复

543 作者的声誉

My Node.js folders hirarchy looks like the next image:

My folders hirarchy

While app.js it's the Node.js main file, routes it's the Node.js routes and src it's the client public html files.

This is the code in app.js:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
global.io = require('socket.io').listen(server);
var compression = require('compression');
var helmet = require('helmet');
var session = require('express-session');

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.use(express.static(__dirname + '/src'));

app.use(helmet());
app.use(compression());

app.use('/rides', require('./routes/ridesServer'));
app.use('/user', require('./routes/userServer'));
app.use('/offers', require('./routes/offersServer'));
app.use('/notifications', require('./routes/notificationsServer'));

server.listen("8080", function() {
  console.log("Connected to db and listening on port 8080");
});

This is another API in routes/userServer.js file:

router.post('/verifytoken', function(req, res, next) {

  // some functions here

});

And this another HTTP REQUEST I am doing from client side, in page: ride.js:

$.ajax({
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  url: "user/verifytoken",
  data: JSON.stringify(something),
  success: function(response) {
    // some code
  },
  error: function(error) {
    // some code
  }
});

As you can see, client files and Node.js server files are on the same server, and Node.js serves those static files via this command:

app.use(express.static(__dirname + '/src'));

I think, that it should be avoided, and there is a better way!

If you are a Node.js expert and familier with best practices, please, tell me if the next method of working is correct, if it does not, please correct me:

I thought about putting static files on public_html directory and Node.js files in server directory which is under public_html directory.

Then run pm2 start app.js --watch or, node app.js on the app.js which is located in server directory, and not in public_html.

In result, index.html file will be served as just as another static file without any relation to Node.js server, and Node.js will be in its own folder, not dealing with any kind of client the side.

In other words, seperate Node.js and static files and put Node.js files as a sub directory and not main directory.

Then the HTTP REQUEST will be looking like this:

$.ajax({
  method: "POST",
  headers: {
    "Content-Type": "application/json"
  },
  url: "server/user/verifytoken",
  data: JSON.stringify(something),
  success: function(response) {
    // some code
  },
  error: function(error) {
    // some code
  }
});

Please note that I have added SERVER directory. Furthermore, I can exchange the

url: "server/user/verifytoken",

to an IP from a remote app (like Ionic):

url: "123.123.123.123:443/server/user/verifytoken",

And then my HTTP REQUESTS will be served via HTTPS (because I am sending for port 443), I can create multiple apps on the same server and I have no struggles with any Node.js express static folders.

What do you think?

Thanks!

作者: Raz 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

1937 作者的声誉

决定

First let me say I'm not an expert. But I have 3 years of continuous development of Node.js based solutions.

In the past I have created solutions mixing client side code and server side code on the same project and it has work. At least for a while. But in the long run is a bad idea for many possible reasons. Some of them are:

  • Client side code and server side code may require different processes to produce working code. For example client side code may require trans compiling from ES6 to more compatible ES5 using something as gulp or Webpack. This is normally not the case for server side code because the runtime is more targeted.
  • Mixing client side code and an API server may prevent you from horizontally scaling one of them without the other.
  • This is like a mono repo. And having a mono repo without a CI process tailor for this scenario may produce very long development times.

What we currently do at my work is as follow:

  • Create a separated API server project. This way you can concentrate on developing a good API while working on this specific project. Let cross-cutting concerns (like authentication) outside the API server.
  • Create a separated project for your client side code (SPA maybe). Set your dev environment to proxy API requests to a running API server (may be running locally).
  • Create a separated project for the deployment of the entire solution. This project will put together the serving of the client code, proxying requests to the API and implementing cross-cutting concerns like authentication, etc.

Having your code separated in this way makes easy developing each pieces and fast evolution. But it may introduce some complexities:

  • This multi-project structure require you to be able to trigger testing the hole product each time one of the project changes.
  • It surface the need of integration testing

Some other considerations are:

  • API server and Website server may run on the same machine but in different ports.
  • You may secure your API server using SSL (on node using the standard https module) but notice that in all cases you need another actor in front of the API server (a website proxying requests to the actual API server of a API gateway that implement cross-cutting concerns like authentication, rate limiting, etc). In the past I pose the same question you have made yourself regarding the apropriate of using SSL in this scenario and the answer is here. My answer is: depends on the deployment conditions.
作者: yeiniel 发布者: 2017 年 12 月 27 日
32x32