Kselax.ru

Hacker Kselax – the best hacker in the world

Menu
  • Blog
  • Contacts
  • wp plugin generator
  • English
    • Русский
Menu

Where to start creating a node.js app. The detailed instruction for a newbie.

Posted on 31 October, 201831 October, 2018 by admin

How to create a node.js app and where to start the newbies often ask these questions. In this article, we’ll try figuring out how it is going on. Node.js app has a few parts. It has always a config file.

 

The content table:

    1. Create a config file

 

#Create a config file

We use a config file for put there options for different services like mysql, redis, steam, and other data. You can put there whatever you want they have to be accessible from anywhere of your site and will be edited in one place.

For a config file, we can use YAML it’s a special language that uses for creating configs link.

We will use two npm packages:

  1. config link
  2. js-yaml link

The config package will search a config file by following this hierarchy link. We have to create a local.yml file somewhere and specify an environment variable NODE_CONFIG_DIR to this folder. The config will search the file there. To add an environment variable we can use a npm scripts section. Put there this code
“start”: “NODE_CONFIG_DIR=./config node index.js”
it will create add a variable NODE_CONFIG_DIR with a path that you can change whatever you want. The config package will check this path and make search config file. For reading YAML files you have to install js-yaml.

That’s it. Here is a code (To run the code use npm start)

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
  "name": "test1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "NODE_CONFIG_DIR=./config node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "config": "^2.0.1",
    "js-yaml": "^3.12.0"
  }
}

 

/config/local.yml

1
2
3
4
5
6
APP:
  PORT: 3000
  TITLE: 'Hello world!'
MYSQ:
  HOST: 'localhost'
  PASS: 'password'

 

index.js

1
2
3
const Config = require('config');
 
console.log('Config = ', Config);

 

console

1
2
3
4
5
6
7
8
9
neo@neo:~/node.js/React/test1$ npm start
 
> test1@1.0.0 start /home/neo/node.js/React/test1
> NODE_CONFIG_DIR=./config node index.js
 
Config =  Config {
  APP: { PORT: 3000, TITLE: 'Hello world!' },
  MYSQ: { HOST: 'localhost', PASS: 'password' } }
neo@neo:~/node.js/React/test1$

 

 

 

 

the end

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • bash (1)
  • English (9)
  • JavaScript (4)
  • node.js (22)
  • photoshop (1)
  • php (3)
  • React (9)
  • sclerotic (6)
  • Ubuntu (10)
  • Uncategorized (13)
  • Wordpress (1)

Tags

Ajax apache2 automation bash chrome-extension command line editor ejs email English English-grammar framework functions git graphql handlebars hybrid app installation javascript js linux newbie node.js node.js javascript nodemailer npm objects Performance php phpmyadmin playonlinux promise rabbitmq React react-router redis reverse-proxy session shell socket.io sublime text 3 time zones ubuntu unity webpack

Recent Comments

  • damien on How to install npm and nodejs the latest versions on ubuntu
  • Cam on How to install npm and nodejs the latest versions on ubuntu
  • Pierre on socket.io with apache as a reverse proxy on the CentOS
  • admin on How to use react-router with a few languages
  • admin on How to install npm and nodejs the latest versions on ubuntu
©2021 Kselax.ru Theme by ThemeGiant