Javascript server side?

2019-03-03 10:29发布

问题:

Is it at all possible to use javascript server side and thus not shown in page source for certain core calculations?

I'm developing a game, and one crucial part of the code needs to be only calculated server side then passed back to client.

My entire game is in Javascript - but most of it is client side which is fine except this one particular function which needs to hidden from view to prevent cheats.

Is this possible - if so how would I set up server side JS ?

回答1:

You'll want to look at node.js. Node.js allows you to run javascript code on the server and is very easy to get up and running to accept and respond to http requests. You'll probably also want to take a look at expressjs which makes implementing a Node.js server even easier.



回答2:

There are a variety of ways of doing server side scripting. Node.js is one solution, but even PHP, Ruby, or Python will let you do server side calculations.

It doesn't matter though, because if your game is heavily client side JS, anyone with a greasemonkey plugin installed can cheat.



回答3:

I would do an AJAX request. Submit raw data to the server and get back the completed data. Use a server side language (PHP) to do the secure calculations. JavaScript on the server side is a real crap shoot.



回答4:

I would also recommend you to check out NowJS (http://nowjs.com/) which will allow you to easily call server functions from client (without exposing their source code).

now.calculateGameScore(function(data){ ... });