Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if js code is running on node server or on the client

I want to determine whether my js code is running on the node server or on the client, and store it into a variable isServer = true if it is running on the server. How can I do that? Is there a way to simply check for the existence of a property that is only available on the server and, if this is possible, which is the best option?

Thanks for help!

like image 313
Pascal Bayer Avatar asked Nov 30 '12 11:11

Pascal Bayer


People also ask

Is JavaScript run on the client or server?

JavaScript. JavaScript is a client-side script, meaning the browser processes the code instead of the web server. Client-side scripts are commonly used when we want to validate data before sending it to the web server, adjusting the interface in response to user feedback, and for implementing other advanced features.

How do I know if node js server is running?

To check the node server running by logging in to the system In windows you can simply go to the Task Manager and check for node in the application list. If it is there then it is running in the machine.

Where does node JS run client or server?

Node. js executes JavaScript code in its environment on the server, whereas Angular is a JavaScript framework that gets executed on the client (i.e. within a web browser.)

Does node JS run on client side?

js modules from JavaScript running in the browser has many advantages because it allows you to use Node. js modules for client-side JavaScript applications without having to use a server with Node. js just to implement functionality that is already available as a Node. js module that is available via an npm package.


1 Answers

You could use this:

function is_server() {
   return ! (typeof window != 'undefined' && window.document);
}

As the global window.document object is only present in the browser context.

like image 119
Nelson Avatar answered Oct 17 '22 00:10

Nelson