{"id":3664,"date":"2022-04-18T06:27:00","date_gmt":"2022-04-18T06:27:00","guid":{"rendered":"https:\/\/blog.paranoidprofessor.com\/?p=3664"},"modified":"2022-04-17T20:21:25","modified_gmt":"2022-04-17T20:21:25","slug":"rest-services-the-node-js-simple-server","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2022\/04\/18\/rest-services-the-node-js-simple-server\/","title":{"rendered":"REST services &#8211; the Node JS simple server"},"content":{"rendered":"\n<p>NodeJS has actually encapsulated all of the hard work.  A simple server that will wait for HTTP GET statements. <\/p>\n\n\n\n<p>This simple server program has a lot of functionality.    It is an example of three different endpoints that can be called.  The \/hello and \/goodbye are pretty obvious and simply return a json message to the caller.  <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const express = require('express')\nlet app = express()\napp.use(express.json());\n\n\n\napp.get('\/hello', (request, response) =&gt; {\n&nbsp; response.json({ \"message\": \"hello world\" });\n});\n\napp.get('\/goodbye', (request, response) =&gt; {\n&nbsp; response.status(200).send(\"goodbye world\");\n});\n\napp.get('\/x\/:id', (request, response) =&gt; {\n&nbsp; response.status(200).send(request.params.id );\n});\n\nconst port = process.env.PORT || 3000;\napp.listen(port, () =&gt; console.log(`listing on port ${port}` ));<\/pre>\n\n\n\n<p>The third endpoint is a bit more interesting as it allows you to pass in a parameter and easily as part of the URL.  This particular example might be good for retrieving a value (ie book) by the code that is passed in.  Because this is a service and not a web server it is only returning a value and not a webpage.  But this is pretty useful as a backend server for your website.<\/p>\n\n\n\n<p>Just as important as the ability to get information it is important to be able to pass in a complex structure.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">app.post('\/newitem', (request, response) =&gt; {\n\n  let body = request.body;\n  console.log(\"body of post\")\n  console.log(body);\n\n\n  var keys = Object.keys(body);\n  for (i in keys)\n    console.log(i,keys[i],body[keys[i]]);\n\n&nbsp; response.status(200).send(request.params.id );\n});<\/pre>\n\n\n\n<p>This example post endpoint allows us to pass in any structure and print it out to the console. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In my next post I will expand on this for other features that might be useful.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NodeJS has actually encapsulated all of the hard work. A simple server that will wait for HTTP GET statements. This simple server program has a lot of functionality. It is an example of three different endpoints that can be called. &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2022\/04\/18\/rest-services-the-node-js-simple-server\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/3664"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=3664"}],"version-history":[{"count":4,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/3664\/revisions"}],"predecessor-version":[{"id":3707,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/3664\/revisions\/3707"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=3664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=3664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=3664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}