{"id":8571,"date":"2015-01-16T21:10:17","date_gmt":"2015-01-16T21:10:17","guid":{"rendered":"https:\/\/wp.okra.host\/article\/running-node-js\/"},"modified":"2021-03-07T14:30:33","modified_gmt":"2021-03-07T13:30:33","slug":"running-node-js","status":"publish","type":"ht_kb","link":"https:\/\/kb.okra.host\/de\/article\/running-node-js\/","title":{"rendered":"Node.js ausf\u00fchren"},"content":{"rendered":"<h2 id=\"overview\" >Overview<\/h2>\n<p><a href=\"http:\/\/nodejs.org\/\">Node.js<\/a> is a performant JavaScript backend built off Chrome&#8217;s JavaScript engine (<a href=\"http:\/\/code.google.com\/p\/v8\/\">v8<\/a>). It&#8217;s also wicked fast. Node.js and its accompanying package management, <a href=\"https:\/\/www.npmjs.com\/\">npm<\/a>, are available on <a title=\"Determining platform version\" href=\"https:\/\/kb.okra.host\/platform\/determining-platform-version\/\">newer platforms<\/a> (v6+) without any <a title=\"Compiling programs\" href=\"https:\/\/kb.okra.host\/terminal\/compiling-programs\/\">additional compilation<\/a> from source. Accounts <a title=\"Is terminal access available?\" href=\"https:\/\/kb.okra.host\/terminal\/is-terminal-access-available\/\">with terminal access<\/a> are eligible to use Node.js and npm.<\/p>\n<h2 id=\"passenger\" id=\"running-node-js-with-passenger\" >Running Node.js with Passenger<\/h2>\n<p>Newer hosting servers, <a title=\"Determining platform version\" href=\"https:\/\/kb.okra.host\/platform\/determining-platform-version\/\">v6+ and above<\/a>, support running Node.js through Passenger. Passenger automatically manages launching Node.js and scaling the number of Node.js instances\u00a0to meet demand.\u00a0To adapt a\u00a0Node.js script to Passenger, create a <a title=\"Passenger application layout\" href=\"https:\/\/kb.okra.host\/cgi-passenger\/passenger-application-layout\/\">compatible<\/a>\u00a0filesystem layout:<\/p>\n<pre>nodejsapp\r\n+-- app.js  &lt;-- main file\r\n+-- public  &lt;-- document root\r\n\u00a6\u00a0\u00a0 +-- .htaccess &lt;-- htaccess control file\r\n+-- tmp     &lt;-- passenger control\/scratch directory<\/pre>\n<p>Create a <a title=\".htaccess Guide\" href=\"https:\/\/kb.okra.host\/guides\/htaccess-guide\/\">.htaccess<\/a> file in <code>public\/<\/code>, which serves as\u00a0the <a title=\"Where is site content served from?\" href=\"https:\/\/kb.okra.host\/web-content\/where-is-site-content-served-from\/\">document root<\/a>,\u00a0with the following lines:<\/p>\n<pre>PassengerNodejs \/usr\/bin\/node<\/pre>\n<p><strong>Note<\/strong> (<em><a href=\"https:\/\/kb.okra.host\/platform\/determining-platform-version\/\">v6.5+ platforms<\/a><\/em>): if the system version is insufficient, use <a href=\"https:\/\/kb.okra.host\/node\/changing-node-versions\/\">nvm<\/a> to specify or install a different Node interpreter. When specifying the path to <code>PassengerNodejs<\/code>, be sure to expand the tilde (~) to your <a href=\"https:\/\/kb.okra.host\/platform\/home-directory-location\/\">home directory<\/a>.<\/p>\n<p><strong>Note:<\/strong>\u00a0(<em>v6 platforms<\/em>) if the system version is insufficient, you may use your own Node.js version installed under \/usr\/local\/bin. Change <em>PassengerNodejs<\/em> from <code>\/usr\/bin\/node<\/code> to <code>\/usr\/local\/bin\/node<\/code>.<\/p>\n<p>Next,\u00a0rename the main file to <code>app.js<\/code> and locate this under public\/ as in the directory layout.\u00a0Connect the public\/ folder to a subdomain or domain within the <a title=\"Logging into the control panel\" href=\"https:\/\/kb.okra.host\/control-panel\/logging-into-the-control-panel\/\">control panel<\/a> and you&#8217;re all set. You can specify another entry-point via the\u00a0<em>PassengerStartupFile<\/em> directive.<\/p>\n<p>You can restart Node.js using the same <a title=\"Restarting Passenger processes\" href=\"https:\/\/kb.okra.host\/ruby\/restarting-passenger-processes\/\">restart mechanism<\/a> as with Ruby or Python scripts.<\/p>\n<h3 id=\"specifying-another-startup\" >Specifying another startup<\/h3>\n<p>In the .htaccess file, specify: <code>PassengerStartupFile <em>newfile.js<\/em><\/code> where\u00a0<em>newfile.js<\/em> is the location\u00a0to another file not named app.js.<\/p>\n<h2 id=\"running-node-js-standalone\" >Running Node.js standalone<\/h2>\n<h3 id=\"quickstart\" >Quickstart<\/h3>\n<p>The following\u00a0lines of code should be added to a file called <code>server.js<\/code>. Replace <code>40201<\/code> with a <a title=\"Listening on ports\" href=\"https:\/\/kb.okra.host\/terminal\/listening-ports\/\">port preallocated<\/a> to your account.<\/p>\n<pre data-language=\"javascript\"><code>\/\/ Load the http module to create an http server.\r\nvar http = require('http');\r\n\r\n\/\/ Configure our HTTP server to respond with Hello World to all requests.\r\nvar server = http.createServer(function (request, response) {\r\n response.writeHead(200, {\"Content-Type\": \"text\/plain\"});\r\n response.end(\"Hello Worldn\");\r\n});\r\n\r\n\/\/ Listen on port 40201, pre-allocated , IP defaults to 127.0.0.1\r\nserver.listen(40201);\r\n\r\n\/\/ Put a friendly message on the terminal\r\nconsole.log(\"Server running at http:\/\/127.0.0.1:40201\/\");<\/code><\/pre>\n<p>A quick and easy way to do this is with Vim, a text-editor available through the terminal:<\/p>\n<ol>\n<li><code>vim ~\/myserver.js<\/code><\/li>\n<li>Type <code>i<\/code>\u00a0on the keyboard to switch to &#8220;Insert&#8221; mode\n<ul>\n<li>Depending upon\u00a0client, paste the text through CTRL + V, Shift + INS, or a suitable\u00a0key combination<\/li>\n<\/ul>\n<\/li>\n<li>Hit the Esc(ape) key.<\/li>\n<li>Type\u00a0<code>:wq<\/code><\/li>\n<li><em>Done!<\/em><\/li>\n<\/ol>\n<p>Now to start Node.js\u00a0using the above server script, type: <code>node ~\/server.js:<\/code><\/p>\n<figure>\n<div class=\"terminal\">\n<div class=\"terminal-text\"><code>[myuser\u00a0~]$ node server.js<br \/>\nServer running at http:\/\/127.0.0.1:40201\/<\/code><\/div>\n<\/div>\n<\/figure>\n<p>Congratulations! Your Node.js\u00a0server is running. You can send a simple request using <a href=\"http:\/\/apnscp.com\/linux-man\/man1\/curl.1.html\">curl<\/a>\u00a0with <code>curl http:\/\/127.0.0.1:40201\/<\/code> to confirm everything is working:<\/p>\n<figure>\n<div class=\"terminal\">\n<div class=\"terminal-text\"><code>[myuser\u00a0~]$\u00a0curl http:\/\/127.0.0.1:40201<br \/>\nHello World!<br \/>\n<\/code><\/div>\n<\/div>\n<\/figure>\n<h3 class=\"clearfix\" id=\"persisting-a-server\" >Persisting a server<\/h3>\n<p>Use <a href=\"https:\/\/www.npmjs.com\/package\/forever\">forever<\/a> through npm (<code>npm install -g forever<\/code>) or <a href=\"http:\/\/apnscp.com\/linux-man\/man1\/nohup.1.html\">nohup<\/a> to run keep a server running even after logging out: <code>nohup node server.js &amp;<\/code><\/p>\n<h3 id=\"starting-on-start-up\" >Starting on Start-up<\/h3>\n<ol>\n<li>Visit <strong>Dev<\/strong> &gt;\u00a0<strong>Task Scheduler<\/strong> within the <a title=\"Logging into the control panel\" href=\"https:\/\/kb.okra.host\/control-panel\/logging-into-the-control-panel\/\">control panel<\/a>\u00a0to schedule a new task.<\/li>\n<li>Under\u00a0<strong>Command<\/strong>, enter <code>node ~\/server.js<\/code><\/li>\n<li>Under <em>Scheduling<\/em>, select\u00a0<strong>Server Start<\/strong><\/li>\n<li>Click\u00a0<strong>Add<\/strong><\/li>\n<\/ol>\n<h2 id=\"passenger\" id=\"_\" ><\/h2>\n<h2 id=\"npm\" id=\"installing-packages\" >Installing\u00a0packages<\/h2>\n<p>Use npm to install packages. Syntax is of the form <code>npm install -g PKGNAME<\/code> where <em>PKGNAME<\/em> is a package <a href=\"https:\/\/www.npmjs.com\/\">listed through npm<\/a>.<\/p>\n<h3 id=\"configuring-global-install-on-older-platforms\" >Configuring global install on older platforms<\/h3>\n<p>Platforms <a title=\"Determining platform version\" href=\"https:\/\/kb.okra.host\/platform\/determining-platform-version\/\">older than v6<\/a> will require a <a href=\"https:\/\/docs.npmjs.com\/files\/npmrc\">.npmrc<\/a> file present within the home directory to define 2 variables, <code>prefix<\/code> and <code>link<\/code>. These 2 variables will set the location in which binaries are installed and\u00a0make a link so the binary appears in your shell path:<\/p>\n<pre>prefix = \/usr\/local\r\nlink = yes<\/pre>\n<p>Once this file is present in your home directory with those 2 lines, then <code>node install -g<\/code> will properly install packages under <code>\/usr\/local<\/code> instead of within the current working directory.<\/p>\n<h2 id=\"see-also\" >See also<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.npmjs.com\/\">npm package repository<\/a><\/li>\n<li><a href=\"http:\/\/nodejs.org\/api\/\">Node.js documentation<\/a><\/li>\n<li><a href=\"http:\/\/www.nodebeginner.org\/\">The Node Beginner Book<\/a><\/li>\n<li><a href=\"http:\/\/nodejs.futz.net\">Node.js launched with Passenger on a v6 platform<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Overview Node.js is a performant JavaScript backend built off Chrome&#8217;s JavaScript engine (v8). It&#8217;s also wicked fast. Node.js and its accompanying package management, npm, are available on newer platforms (v6+) without any additional compilation from source. Accounts with terminal access are eligible to use Node.js and npm. Running Node.js with&#8230;<\/p>","protected":false},"author":1,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"ht-kb-category":[60],"ht-kb-tag":[],"class_list":["post-8571","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-guides"],"_links":{"self":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/comments?post=8571"}],"version-history":[{"count":1,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8571\/revisions"}],"predecessor-version":[{"id":8572,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb\/8571\/revisions\/8572"}],"wp:attachment":[{"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/media?parent=8571"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-category?post=8571"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/kb.okra.host\/de\/wp-json\/wp\/v2\/ht-kb-tag?post=8571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}