{"id":7465,"date":"2019-09-28T18:27:48","date_gmt":"2019-09-28T18:27:48","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=7465"},"modified":"2019-09-28T18:32:14","modified_gmt":"2019-09-28T18:32:14","slug":"express-server-with-typescript","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/express-server-with-typescript\/","title":{"rendered":"Express server with TypeScript"},"content":{"rendered":"<p>Express is a popular web application server in JavaScript. <\/p>\n<p>Let&#8217;s create a really quick and simple expression server which will be a good starting point for further posts which use this library.<\/p>\n<p>I&#8217;ll assume you&#8217;ve created a folder, run yarn init and tsc &#8211;init, next up run<\/p>\n<ul>\n<li>yarn add express<\/li>\n<li>yarn add -D @types\/express<\/li>\n<li>Add a new file, mine&#8217;s named server.ts<\/em>\n<li>Add the usual scripts\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;scripts&quot;: {\r\n  &quot;build&quot;: &quot;tsc&quot;,\r\n  &quot;start&quot;: &quot;node server.js&quot;\r\n}\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>The code to run up a really simple server is as follows<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport express from &quot;express&quot;;\r\n\r\nconst server = express();\r\n\r\nserver.get(&quot;\/&quot;, (request, response) =&gt; {\r\n  response.send(&quot;&lt;h1&gt;Hello World&lt;\/h1&gt;&quot;);\r\n});\r\n\r\nconst port = 4000;\r\nserver.listen(port, \r\n  () =&gt; console.log(`Server on port ${port}`)\r\n);\r\n<\/pre>\n<p>Using our scripts, run <em>yarn build<\/em> followed by <em>yarn start<\/em>. The server should start on port 4000, so now navigate to http:\/\/localhost:4000 using your preferred browser and that&#8217;s it.<\/p>\n<p>We&#8217;ve created a server instance on port 4000, we&#8217;re routed any root (i.e. \/) calls to the response, which in this case returns a simple HTML string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Express is a popular web application server in JavaScript. Let&#8217;s create a really quick and simple expression server which will be a good starting point for further posts which use this library. I&#8217;ll assume you&#8217;ve created a folder, run yarn init and tsc &#8211;init, next up run yarn add express yarn add -D @types\/express Add [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[268,45,46],"tags":[],"class_list":["post-7465","post","type-post","status-publish","format-standard","hentry","category-express","category-javascript","category-typescript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=7465"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7465\/revisions"}],"predecessor-version":[{"id":7468,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/7465\/revisions\/7468"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=7465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=7465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=7465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}