{"id":7142,"date":"2023-04-03T09:51:19","date_gmt":"2023-04-03T04:21:19","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7142"},"modified":"2023-04-06T13:25:26","modified_gmt":"2023-04-06T07:55:26","slug":"how-to-start-a-node-on-the-server","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-start-a-node-on-the-server\/","title":{"rendered":"How to start a node on the server"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Node.js is a JavaScript runtime environment that allows developers to write server-side applications using the same language as client-side web development. It uses an event-driven, non-blocking I\/O model that makes it efficient and scalable, even for handling large amounts of data in real-time. Its rich library of modules and tools makes it a popular choice for building web APIs and microservices.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Prerequisites<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SSH login details<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Procedure<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: <\/strong>Login to the server via ssh<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#000000\" class=\"has-inline-color has-white-color\">$ ssh username@IP<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong> Install the Node.js stable version.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#000000\" class=\"has-inline-color has-white-color\">$ sudo apt install nodejs -y<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above command is used for ubuntu os, For centos you can use <strong>yum<\/strong> instead of <strong>apt<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3:<\/strong> Verify the installed Node.js version.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#000000\" class=\"has-inline-color has-white-color\">$ sudo nodejs -v<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4: <\/strong>Create the sample .js file using the command as mentioned below,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#000000\" class=\"has-inline-color has-white-color\">$ sudo vim test.js<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5: <\/strong>Copy paste the below content in that test.js file<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>const http = require(&#8216;http&#8217;);<br>const hostname = &#8216;127.0.0.1&#8217;;<br>const port = 3000;<br><br>const server = http.createServer((req, res) =&gt; {<br>&nbsp;&nbsp;res.statusCode = 200;<br>&nbsp;&nbsp;res.setHeader(&#8216;Content-Type&#8217;, &#8216;text\/plain&#8217;);<br>&nbsp;&nbsp;res.end(&#8216;<code>Hello World!<\/code>&#8216;);<br>});<br><br>server.listen(port, hostname, () =&gt; {<br>&nbsp;&nbsp;console.log(`Server running at http:\/\/${hostname}:${port}\/`);<br>});<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 6:<\/strong> Start the node using the following command, <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#000000\" class=\"has-inline-color has-white-color\">$ sudo node test.js<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now the node gets started.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 7:<\/strong> &nbsp;If the port is blocked in the firewall run the below command to allow the port,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#000000\" class=\"has-inline-color has-white-color\">$ sudo iptables -I INPUT 1 -p tcp &#8211;dport 3000 -j ACCEPT<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 8: <\/strong>Then try to start a node, using the node command below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#000000\" class=\"has-inline-color has-white-color\">$ sudo node test.js<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open a browser and enter url&nbsp;<a href=\"http:\/\/127.0.0.1:3000\/\">http:\/\/127.0.0.1:3000\/<\/a>. The browser will display&nbsp;<strong><code>Hello World!<\/code><\/strong>&nbsp;message on the screen.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/04\/image.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"555\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/04\/image-1024x555.png\" alt=\"\" class=\"wp-image-7143\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/04\/image-1024x555.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/04\/image-300x163.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/04\/image-768x416.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/04\/image-553x300.png 553w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/04\/image.png 1293w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You have installed Node.js and successfully started the node on server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Node.js is a JavaScript runtime environment that allows developers to write server-side applications using the same language as client-side [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[261],"class_list":["post-7142","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-linux","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Rc","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7142"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7142\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}