|
1 | 1 | # Experiences data service
|
2 | 2 |
|
3 |
| -### Getting Started |
4 |
| - |
5 |
| -First we'll install `@babel/cli`, `@babel/core` and `@babel/preset-env`. |
6 |
| - |
7 |
| -```shell |
8 |
| -$ npm install --save-dev @babel/cli @babel/core @babel/preset-env |
9 |
| -``` |
10 |
| - |
11 |
| -Then we'll create a `.babelrc` file for configuring babel. |
12 |
| - |
13 |
| -```shell |
14 |
| -$ touch .babelrc |
15 |
| -``` |
16 |
| - |
17 |
| -This will host any options we might want to configure `babel` with. |
18 |
| - |
19 |
| -```json |
20 |
| -{ |
21 |
| - "presets": ["@babel/preset-env"] |
22 |
| -} |
23 |
| -``` |
24 |
| - |
25 |
| -Then create our server in `index.js`. |
26 |
| - |
27 |
| -```shell |
28 |
| -$ touch index.js |
29 |
| -``` |
30 |
| -```js |
31 |
| -import http from 'http'; |
32 |
| - |
33 |
| -const server = http.createServer((req, res) => { |
34 |
| - res.writeHead(200, {'Content-Type': 'text/plain'}); |
35 |
| - res.end('Hello World\n'); |
36 |
| -}).listen(1337, '127.0.0.1'); |
37 |
| - |
38 |
| -console.log('Server running at http://127.0.0.1:1337/'); |
39 |
| - |
40 |
| -export default server; |
41 |
| -``` |
42 |
| - |
43 |
| -With recent changes to babel, you will need to transpile your ES6 before node can run it. |
44 |
| - |
45 |
| -So, we'll add our first script, `build`, in `package.json`. |
46 |
| - |
47 |
| -```diff |
48 |
| - "scripts": { |
49 |
| -+ "build": "babel index.js -d dist" |
50 |
| - } |
51 |
| -``` |
52 |
| - |
53 |
| -Then we'll add our `start` script in `package.json`. |
54 |
| - |
55 |
| - |
56 |
| -```diff |
57 |
| - "scripts": { |
58 |
| - "build": "babel index.js -d dist", |
59 |
| -+ "start": "npm run build && node dist/index.js" |
60 |
| - } |
61 |
| -``` |
62 |
| - |
63 |
| -Now let's start our server. |
64 |
| - |
65 |
| -```shell |
66 |
| -$ npm start |
67 |
| -``` |
68 |
| - |
69 |
| -You should now be able to visit `http://127.0.0.1:1337` and see `Hello World`. |
70 |
| - |
71 |
| -### Watching file changes with `nodemon` |
72 |
| - |
73 |
| -We can improve our `npm start` script with `nodemon`. |
74 |
| - |
75 |
| -```shell |
76 |
| -$ npm install --save-dev nodemon |
77 |
| -``` |
78 |
| - |
79 |
| -Then we can update our `npm start` script. |
80 |
| - |
81 |
| -```diff |
82 |
| - "scripts": { |
83 |
| - "build": "babel index.js -d dist", |
84 |
| -- "start": "npm run build && node dist/index.js" |
85 |
| -+ "start": "npm run build && nodemon dist/index.js" |
86 |
| - } |
87 |
| -``` |
88 |
| - |
89 |
| -Then we'll restart our server. |
90 |
| - |
91 |
| -```shell |
92 |
| -$ npm start |
93 |
| -``` |
94 |
| - |
95 |
| -You should now be able to make changes to `index.js` and our server should be |
96 |
| -restarted automatically by `nodemon`. |
97 |
| - |
98 |
| -Go ahead and replace `Hello World` with `Hello {{YOUR_NAME_HERE}}` while our |
99 |
| -server is running. |
100 |
| - |
101 |
| -If you visit `http://127.0.0.1:1337` you should see our server greeting you. |
102 |
| - |
103 |
| -### Getting ready for production use |
104 |
| - |
105 |
| -First let's move our server `index.js` file to `lib/index.js`. |
106 |
| - |
107 |
| -```shell |
108 |
| -$ mkdir lib |
109 |
| -$ mv index.js lib/index.js |
110 |
| -``` |
111 |
| - |
112 |
| -And update our `npm start` script to reflect the location change. |
113 |
| - |
114 |
| -```diff |
115 |
| - "scripts": { |
116 |
| -- "build": "babel index.js -d dist", |
117 |
| -+ "build": "babel lib -d dist", |
118 |
| - "start": "npm run build && nodemon dist/index.js" |
119 |
| - } |
120 |
| -``` |
121 |
| - |
122 |
| -Next let's add a new task: `npm run serve`. |
123 |
| - |
124 |
| -```diff |
125 |
| - "scripts": { |
126 |
| - "build": "babel lib -d dist", |
127 |
| - "start": "npm run build && nodemon dist/index.js", |
128 |
| -+ "serve": "node dist/index.js" |
129 |
| - } |
130 |
| -``` |
131 |
| - |
132 |
| -Now we can use `npm run build` for precompiling our assets, and `npm run serve` |
133 |
| -for starting our server in production. |
134 |
| - |
135 |
| -```shell |
136 |
| -$ npm run build |
137 |
| -$ npm run serve |
138 |
| -``` |
139 |
| - |
140 |
| -This means we can quickly restart our server without waiting for `babel` to |
141 |
| -recompile our files. |
142 |
| - |
143 |
| -Oh, let's not forget to add `dist` to our `.gitignore` file: |
144 |
| - |
145 |
| -```shell |
146 |
| -$ touch .gitignore |
147 |
| -``` |
148 |
| - |
149 |
| -``` |
150 |
| -dist |
151 |
| -``` |
152 |
| - |
153 |
| -This will make sure we don't accidentally commit our built files to git. |
154 |
| - |
155 |
| -### Testing the server |
156 |
| - |
157 |
| -Finally let's make sure our server is well tested. |
158 |
| - |
159 |
| -Let's install `mocha`. |
160 |
| - |
161 |
| -```shell |
162 |
| -$ npm install --save-dev mocha |
163 |
| -``` |
164 |
| - |
165 |
| -And create our test in `test/index.js`. |
166 |
| - |
167 |
| -```shell |
168 |
| -$ mkdir test |
169 |
| -$ touch test/index.js |
170 |
| -``` |
171 |
| - |
172 |
| -```js |
173 |
| -import http from 'http'; |
174 |
| -import assert from 'assert'; |
175 |
| - |
176 |
| -import server from '../lib/index.js'; |
177 |
| - |
178 |
| -describe('Example Node Server', () => { |
179 |
| - it('should return 200', done => { |
180 |
| - http.get('http://127.0.0.1:1337', res => { |
181 |
| - assert.equal(200, res.statusCode); |
182 |
| - server.close(); |
183 |
| - done(); |
184 |
| - }); |
185 |
| - }); |
186 |
| -}); |
187 |
| -``` |
188 |
| - |
189 |
| -Next, install `@babel/register` for the require hook. |
190 |
| - |
191 |
| -```shell |
192 |
| -$ npm install --save-dev @babel/register |
193 |
| -``` |
194 |
| - |
195 |
| -Then we can add an `npm test` script. |
196 |
| - |
197 |
| -```diff |
198 |
| - "scripts": { |
199 |
| - "start": "nodemon lib/index.js --exec babel-node", |
200 |
| - "build": "babel lib -d dist", |
201 |
| - "serve": "node dist/index.js", |
202 |
| -+ "test": "mocha --require @babel/register" |
203 |
| - } |
204 |
| -``` |
205 |
| - |
206 |
| -Now let's run our tests. |
207 |
| - |
208 |
| -```shell |
209 |
| -$ npm test |
210 |
| -``` |
211 |
| - |
212 |
| -You should see the following: |
213 |
| - |
214 |
| -```shell |
215 |
| -Server running at http://127.0.0.1:1337/ |
216 |
| - |
217 |
| - Example Node Server |
218 |
| - ✓ should return 200 |
219 |
| - |
220 |
| - 1 passing (43ms) |
221 |
| -``` |
222 |
| - |
223 |
| -That's it! |
| 3 | +# Pre-requisites |
| 4 | +- Install [Node.js](https://nodejs.org/en/) version >=8.0.0 |
| 5 | + |
| 6 | + |
| 7 | +# Getting started |
| 8 | +- Clone the repository |
| 9 | + ``` |
| 10 | + git clone https://github.com/mauryakrishna/experiences-data-service experiences-data-service |
| 11 | + ``` |
| 12 | +- Install dependencies |
| 13 | + ``` |
| 14 | + cd experiences-data-service |
| 15 | + npm install |
| 16 | + ``` |
| 17 | +- Build and run the project |
| 18 | + ``` |
| 19 | + npm run dev |
| 20 | + ``` |
| 21 | + Navigate to `http://localhost:4000/gql` to load the GraphQL playground. |
| 22 | + |
| 23 | +- Analyze the build |
| 24 | + ``` |
| 25 | + yarn build-stats |
| 26 | + ``` |
| 27 | + |
| 28 | + Refer to [Experiences client](https://github.com/mauryakrishna/experiences-client) for front-end setup. |
0 commit comments