{"id":204,"date":"2022-05-03T15:12:42","date_gmt":"2022-05-03T15:12:42","guid":{"rendered":"https:\/\/felixaugenstein.com\/blog\/?p=204"},"modified":"2022-05-03T15:12:44","modified_gmt":"2022-05-03T15:12:44","slug":"set-up-express-js-in-node-js-and-build-a-small-application-with-nodemon-express-generator-and-dotenv-libraries","status":"publish","type":"post","link":"https:\/\/felixaugenstein.com\/blog\/set-up-express-js-in-node-js-and-build-a-small-application-with-nodemon-express-generator-and-dotenv-libraries\/","title":{"rendered":"Set up Express.js in Node.js and build a small application with Nodemon, express-generator, and dotenv libraries"},"content":{"rendered":"\n<p>In this tutorial you will set up a new Express.js application with some commonly used libraries like nodemon and dotenv.<\/p>\n\n\n\n<p>Let&#8217;s assume you\u2019ve already installed\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/nodejs.org\/\" target=\"_blank\">Node.js<\/a>, as a next step we will create a new directory for your Express.js application.<\/p>\n\n\n\n<p>In a terminal type the following 2 commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>$ mkdir myExpressApp\n$ cd myExpressApp<\/code><\/code><\/pre>\n\n\n\n<p>Now type the&nbsp;<code>npm init<\/code>&nbsp;command in your terminal to create a&nbsp;<code>package.json<\/code>&nbsp;file for your application. For more information on&nbsp;<code>package.json<\/code>&nbsp;works, see&nbsp;<a href=\"https:\/\/docs.npmjs.com\/files\/package.json\">npm\u2019s package.json documentary<\/a>.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<pre class=\"wp-block-code\"><code>npm init<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>This command prompts you for a number of things, such as the package name, version of your application, description, test command, git repository, etc. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>entry point: (index.js)<\/code><\/pre>\n\n\n\n<p>Enter&nbsp;<code>app.js<\/code>, or whatever you want the name of the main file to be. If you want it to be&nbsp;<code>index.js<\/code>, hit RETURN to accept the suggested default file name. When asked <code>Is this OK? (yes)<\/code> type <code>yes<\/code>.<\/p>\n\n\n\n<p>Now install Express in your app&nbsp;directory and save it in the dependencies list. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install express<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Express-generator<\/h2>\n\n\n\n<p>To install <code>express-generator<\/code>, to create an application skeleton, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g express-generator<\/code><\/pre>\n\n\n\n<p>For more information take a look at <a href=\"https:\/\/www.npmjs.com\/package\/express-generator\" target=\"_blank\" rel=\"noreferrer noopener\">npm&#8217;s express-generator documentary<\/a>.<\/p>\n\n\n\n<p>The <code>express-generator<\/code> can be further configured with the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    --version        output the version number\n-e, --ejs            add ejs engine support\n    --pug            add pug engine support\n    --hbs            add handlebars engine support\n-H, --hogan          add hogan.js engine support\n-v, --view &lt;engine&gt;  add view &lt;engine&gt; support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)\n    --no-view        use static html instead of view engine\n-c, --css &lt;engine&gt;   add stylesheet &lt;engine&gt; support (less|stylus|compass|sass) (defaults to plain css)\n    --git            add .gitignore\n-f, --force          force on non-empty directory\n-h, --help           output usage information<\/code><\/pre>\n\n\n\n<p>For the views, let&#8217;s choose ejs for this application, therefore run this command in your terminal to create the application skeleton:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>express --view=ejs<\/code><\/pre>\n\n\n\n<p>Now you should see the following directory structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\n\u251c\u2500\u2500 app.js\n\u251c\u2500\u2500 bin\n\u2502   \u2514\u2500\u2500 www\n\u251c\u2500\u2500 package.json\n\u251c\u2500\u2500 public\n\u2502   \u251c\u2500\u2500 images\n\u2502   \u251c\u2500\u2500 javascripts\n\u2502   \u2514\u2500\u2500 stylesheets\n\u2502       \u2514\u2500\u2500 style.css\n\u251c\u2500\u2500 routes\n\u2502   \u251c\u2500\u2500 index.js\n\u2502   \u2514\u2500\u2500 users.js\n\u2514\u2500\u2500 views\n    \u251c\u2500\u2500 error.ejs\n    \u2514\u2500\u2500 index.ejs\n\n7 directories, 8 files<\/code><\/pre>\n\n\n\n<p>Install dependencies with the <code>npm install<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install<\/code><\/pre>\n\n\n\n<p>Start your Express.js app at&nbsp;<code>http:\/\/localhost:3000\/<\/code> with the <code>npm start<\/code> command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm start<\/code><\/pre>\n\n\n\n<p>If you wish to change the port, go to <code>bin\/www<\/code> or you can use an environment file later in this tutorial to configure the port.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Nodemon<\/h2>\n\n\n\n<p>So far, you are able to start your application by running the npm start script. The only remaining concern is that you have to restart the app every time you change it. You can change this behavior with an always-running node process. To remedy this, install the commonly used\u00a0<a href=\"https:\/\/www.npmjs.com\/package\/nodemon\">nodemon<\/a>\u00a0library on the command line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install nodemon<\/code><\/pre>\n\n\n\n<p>Next, in your <code>package.json<\/code> file add a new script, to start your app using nodemon:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"scripts\": {\n    \"start\": \"node .\/bin\/www\",\n    \"dev\": \"npx nodemon app.js -e js,ejs,html,css\"\n},<\/code><\/pre>\n\n\n\n<p>Using the script above your app will restart, whenever you save files with the specified extensions. You can now start your app with this command (instead of <code>npm start<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run dev<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Environment variables in Node.js<\/h2>\n\n\n\n<p>It is crucial to set data like private API keys and user credentials like password, username, etc. as environmental variables, but without exposing them in the source code. Therefore, let&#8217;s put environment variables in a dedicated file that is safe from external access. The\u00a0<em>.env<\/em>\u00a0file lets you set Node.js environment variables. On the command line, in your project&#8217;s root folder, create a\u00a0<em>.env<\/em>\u00a0file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch .env<\/code><\/pre>\n\n\n\n<p>Now you can place any key value pair that you don&#8217;t want in your source code in this new file, for instance an API key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>API_KEY=mySecretApiKey123<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.npmjs.com\/package\/dotenv\" target=\"_blank\" rel=\"noreferrer noopener\">dotenv<\/a>&nbsp;is another helpful library to make environmental variables accessible in the source code. First, install it on the command line as a normal dependency:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install dotenv<\/code><\/pre>\n\n\n\n<p>Second, import it into your\u00a0<code>app.js<\/code> or<em> <\/em><code>index.js<\/code><em> <\/em>file to initialize it. The environment variable from your\u00a0<em>.env<\/em>\u00a0file is now accessible in your source code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>require('dotenv').config()\nconsole.log('My API key: ', process.env.API_KEY);<\/code><\/pre>\n\n\n\n<p>An even better option is to create a new module in a file named\u00a0<code>config.js<\/code>. in the root directory of your app, next to <code>app.js<\/code>. Then copy and paste the following code into the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ config.js\nconst dotenv = require('dotenv');\ndotenv.config();\nmodule.exports = {\n  apiKey: process.env.API_KEY,\n  anotherApiKey: process.env.ANOTHER_API_KEY\n};<\/code><\/pre>\n\n\n\n<p>Then in your app.js file you can simply import the module and use credentials like API keys:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ app.js\nconst { apiKey, anotherApiKey } = require(\u2018.\/config\u2019);<\/code><\/pre>\n\n\n\n<p>And that&#8217;s it. Now you have a running express.js application that restarts easily with nodemon, whenever you change files, and that includes the dotenv library to store your credentials safely in an environment file. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial you will set up a new Express.js application with some commonly used libraries like nodemon and dotenv. Let&#8217;s assume you\u2019ve already installed\u00a0Node.js, as a next step we will create a new directory for your Express.js application. In a terminal type the following 2 commands: Now type the&nbsp;npm [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":220,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,44,30],"tags":[],"class_list":["post-204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-application","category-express-js","category-node-js"],"_links":{"self":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":13,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":219,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/posts\/204\/revisions\/219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/media\/220"}],"wp:attachment":[{"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/felixaugenstein.com\/blog\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}