Question

I have used javascript (and jquery) to use google api for its search engine. But now I think it must be done on server side.Using it on client side can expose some critical portion of site to client and make it vulnerable. Is there anyway I can port that javascript code as it is on server side.
I am using php as server side script.

Était-ce utile?

La solution

You probably want to port whatever code you are using to PHP, or rewrite it. There is no easy way to execute JavaScript in PHP, but most often, rewriting the code is fairly easy.

JavaScript can be used on server side, but this requires massively reconfiguring the server, and getting into a completely new technology. It is most likely not what you want.

Autres conseils

You can try using node.js. It's most popular JS web server software which can be use as web server nowadays. Example:

An example of a web server written in Node which responds with "Hello World" for every request.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');

But it means you will have TWO servers - one for PHP and one for JS code. You must think iif it's worth.

On the other hand if you may consider rewriting whole app to JavaScript (I mean PHP server-side part), then you may find it easier to maintain (same language on both server and client side).

There are some javascript server side solutions but a typical way to solve this problem is using web services called by AJAX. Most javascript libraries inlclude a way to do AJAX and you should have no problem finding examples.

If your goal is to save time by using javascript on the server you are going to be in for a shock -- it will take you a lot longer to learn how to and use any server side technology.

Stick with calling a php web service is my suggestion.

If you have to use javascript on the server you should use node -- but as WTP points out this is not a web server itself -- you will still have to call that code as a web service seperate from your site code.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top