1. Create a directory for the project e.g.:
$ mkdir /Projects/meanapp
$ cd /Projects/meanapp
2. Start generating the package.json using app.js as entry point:
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (meanapp)
version: (1.0.0)
description: MEAN app
entry point: (index.js) app.js
test command:
git repository:
keywords:
author: Author
license: (ISC)
About to write to /home/admin/Projects/meanapp/package.json:
{
"name": "meanapp",
"version": "1.0.0",
"description": "MEAN app",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Author",
"license": "ISC"
}
Is this ok? (yes)
3. Below is the package.json generated in the previous step; below in bold is the line added manually in order to allow using the npm start command:
{
"name": "meanapp",
"version": "1.0.0",
"description": "MEAN app",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app"
},
"author": "Author",
"license": "ISC"
}
$ npm install --save express mongoose bcryptjs cors jsonwebtoken body-parser passport passport-jwt
meanapp@1.0.0 /home/admin/Projects/meanapp
├── bcryptjs@2.4.3
├─┬ body-parser@1.16.1
│ ├── bytes@2.4.0
│ ├── content-type@1.0.2
│ ├── debug@2.6.1
│ ├── depd@1.1.0
│ ├─┬ http-errors@1.5.1
│ │ ├── inherits@2.0.3
│ │ ├── setprototypeof@1.0.2
│ │ └── statuses@1.3.1
│ ├── iconv-lite@0.4.15
│ ├─┬ on-finished@2.3.0
│ │ └── ee-first@1.1.1
│ ├── qs@6.2.1
│ ├─┬ raw-body@2.2.0
│ │ └── unpipe@1.0.0
│ └─┬ type-is@1.6.14
│ ├── media-typer@0.3.0
│ └─┬ mime-types@2.1.14
│ └── mime-db@1.26.0
├─┬ cors@2.8.1
│ └── vary@1.1.0
├─┬ express@4.14.1
│ ├─┬ accepts@1.3.3
│ │ └── negotiator@0.6.1
│ ├── array-flatten@1.1.1
│ ├── content-disposition@0.5.2
│ ├── cookie@0.3.1
│ ├── cookie-signature@1.0.6
│ ├─┬ debug@2.2.0
│ │ └── ms@0.7.1
│ ├── encodeurl@1.0.1
│ ├── escape-html@1.0.3
│ ├── etag@1.7.0
│ ├─┬ finalhandler@0.5.1
│ │ └─┬ debug@2.2.0
│ │ └── ms@0.7.1
│ ├── fresh@0.3.0
│ ├── merge-descriptors@1.0.1
│ ├── methods@1.1.2
│ ├── parseurl@1.3.1
│ ├── path-to-regexp@0.1.7
│ ├─┬ proxy-addr@1.1.3
│ │ ├── forwarded@0.1.0
│ │ └── ipaddr.js@1.2.0
│ ├── qs@6.2.0
│ ├── range-parser@1.2.0
│ ├─┬ send@0.14.2
│ │ ├─┬ debug@2.2.0
│ │ │ └── ms@0.7.1
│ │ ├── destroy@1.0.4
│ │ └── mime@1.3.4
│ ├── serve-static@1.11.2
│ └── utils-merge@1.0.0
├─┬ jsonwebtoken@7.3.0
│ ├─┬ joi@6.10.1
│ │ ├── hoek@2.16.3
│ │ ├── isemail@1.2.0
│ │ ├── moment@2.17.1
│ │ └── topo@1.1.0
│ ├─┬ jws@3.1.4
│ │ ├── base64url@2.0.0
│ │ ├─┬ jwa@1.1.5
│ │ │ ├── buffer-equal-constant-time@1.0.1
│ │ │ └── ecdsa-sig-formatter@1.0.9
│ │ └── safe-buffer@5.0.1
│ ├── lodash.once@4.1.1
│ ├── ms@0.7.2
│ └── xtend@4.0.1
├─┬ mongoose@4.8.5
│ ├─┬ async@2.1.4
│ │ └── lodash@4.17.4
│ ├── bson@1.0.4
│ ├── hooks-fixed@1.2.0
│ ├── kareem@1.2.1
│ ├─┬ mongodb@2.2.24
│ │ ├── es6-promise@3.2.1
│ │ ├─┬ mongodb-core@2.1.8
│ │ │ └─┬ require_optional@1.0.0
│ │ │ ├── resolve-from@2.0.0
│ │ │ └── semver@5.3.0
│ │ └─┬ readable-stream@2.1.5
│ │ ├── buffer-shims@1.0.0
│ │ ├── core-util-is@1.0.2
│ │ ├── isarray@1.0.0
│ │ ├── process-nextick-args@1.0.7
│ │ ├── string_decoder@0.10.31
│ │ └── util-deprecate@1.0.2
│ ├── mpath@0.2.1
│ ├── mpromise@0.5.5
│ ├─┬ mquery@2.2.3
│ │ ├── bluebird@2.10.2
│ │ ├─┬ debug@2.2.0
│ │ │ └── ms@0.7.1
│ │ └── sliced@0.0.5
│ ├── muri@1.2.1
│ ├── regexp-clone@0.0.1
│ └── sliced@1.0.1
├─┬ passport@0.3.2
│ ├── passport-strategy@1.0.0
│ └── pause@0.0.1
└── passport-jwt@2.2.1
npm WARN meanapp@1.0.0 No repository field.
5. Install nodemon, an excellent development tool that monitor for any changes in node.js application and automatically restart the server. Use -g to install globally:
$ sudo npm install -g nodemon
/usr/bin/nodemon -> /usr/lib/node_modules/nodemon/bin/nodemon.js
/usr/lib
└─┬ nodemon@1.11.0
├─┬ chokidar@1.6.1
│ ├─┬ anymatch@1.3.0
│ │ └─┬ micromatch@2.3.11
│ │ └─┬ object.omit@2.0.1
│ │ └─┬ for-own@0.1.5
│ │ └── for-in@1.0.1
│ └─┬ readdirp@2.1.0
│ └── readable-stream@2.2.3
└─┬ update-notifier@0.5.0
└─┬ latest-version@1.0.1
└─┬ package-json@1.2.0
└─┬ registry-url@3.1.0
└─┬ rc@1.1.7
└── strip-json-comments@2.0.1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/nodemon/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
6. Create app.js file in the project root directory:
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const users = require('./routes/users');
const dbconfig = require('./config/database');
/* Connect to database */
mongoose.connect(dbconfig.database, (err) => {
if(err){
console.log('Database connection error: ', err);
}
});
mongoose.connection.on('connected', () => {
console.log('Connected to database ' + dbconfig.database);
});
/* Express initialization */
const app = express();
/* Server port number */
const port = 3000;
/* Middleware - CORS */
app.use(cors());
/* Set static folder */
app.use(express.static(path.join(__dirname, 'public')));
/* Middleware - Body parser */
app.use(bodyParser.json());
/* Middleware - Routes */
app.use('/users', users);
/* Root */
app.get('/', (req, res) => {
res.send('Invalid endpoint');
})
/* Start server */
app.listen(port, () => {
console.log('Server started on port '+port);
})
7. The file ./routes/users refers to routes:
const express = require('express');
const router = express.Router();
router.post('/register', (req, res) => {
res.send('REGISTER');
})
router.post('/authenticate', (req, res) => {
res.send('AUTHENTICATE');
})
router.get('/profile', (req, res) => {
res.send('PROFILE');
})
module.exports = router;
8. The file ./config/database refers to the database connection parameters:
module.exports = {
database: 'mongodb://localhost:27017/meanapp',
secret: 'mysecret'
}
References:
- https://youtu.be/DQ9pZ2NKXRo?list=PLillGF-RfqbZMNtaOXJQiDebNXjVapWPZ
- https://github.com/remy/nodemon
- https://www.npmjs.com/package/cors
- https://www.npmjs.com/package/body-parser
I am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing.
ReplyDeleteStart your journey with SAP SD Trainingand get hands-on Experience with 100% Placement assistance from experts Trainers @Softgen Infotech Located in BTM Layout Bangalore.