Is it possible to run a Node.js file from the serverside from JavaScript? If so how would i? I have googled some but cant find a real answer to this. Thanks
3 Answers
NodeJS is JavaScript on the server-side environment.
What you refer as "JavaScript" is actually the same JavaScript, just in your browser environment.
What you can do: you can use Webpack to create a NodeJS app, that will automatically create an index.html and put it on a server. With webpack, you can also import NodeJS modules in your frontend code.
For example:
main.js:
import './index.html';
import $ from 'jquery';
$(() => {
console.log('jquery is ready');
$('#root').append('<h2>How are you doing?</h2>');
});
index.html:
<html>
<head>
</head>
<body>
<div id="root"></div>
<script src="bundle.js"></script>
</body>
</html>
The bundle.js will be generated by Webpack.
Comments
there are some basic differences between browser js env and node server env. Example: WEB API is availble for browser js env for node its different.
Moreover node ecosystem has alot more support for several other functinalities like file system handling etc.
Since node is developed for server side programming,you should pay proper respect to the purpose and not try to run it in browser :)
builtusing something like webpack.