<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nanda kishor palei</title>
    <description>The latest articles on Forem by Nanda kishor palei (@nandakishorpalei).</description>
    <link>https://forem.com/nandakishorpalei</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1025123%2Ffcf7f0e1-760f-40ef-9dab-f890368ba126.jpeg</url>
      <title>Forem: Nanda kishor palei</title>
      <link>https://forem.com/nandakishorpalei</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nandakishorpalei"/>
    <language>en</language>
    <item>
      <title>Node js and Express js project setup</title>
      <dc:creator>Nanda kishor palei</dc:creator>
      <pubDate>Sun, 12 Feb 2023 04:41:12 +0000</pubDate>
      <link>https://forem.com/nandakishorpalei/node-js-and-express-js-project-setup-2lfa</link>
      <guid>https://forem.com/nandakishorpalei/node-js-and-express-js-project-setup-2lfa</guid>
      <description>&lt;p&gt;&lt;strong&gt;Explaining with example of create and get user&lt;/strong&gt;&lt;br&gt;
npm init&lt;/p&gt;

&lt;p&gt;npm install express mongoose bcryptjs dotenv nodemon&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Make 3 folders inside the src folder&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;configs&lt;/li&gt;
&lt;li&gt;controllers&lt;/li&gt;
&lt;li&gt;model&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;two files at the top level&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;index.js&lt;/li&gt;
&lt;li&gt;server.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create a MongoDB database to connect to your app&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open &lt;a href="https://cloud.mongodb.com/v2" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If You don’t have a database already created then follow below instructions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Or feel free to skip these instructions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from the left menu click on the database

click on Build a database

select the type of database you need ( for learning select free )

then click Create

click on create cluster

add a username and password ( do remember the password )

create user

scroll down and select cloud environment

then add an IP address by clicking Add My Current IP Address button

scroll down and click finish and close
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Then your database is ready to serve&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;click on the connect button on the current cluster

click on connect to your application

copy the link

mongodb+srv://nk45:&amp;lt;password&amp;gt;@cluster0.buxffaj.mongodb.net/?retryWrites=true&amp;amp;w=majority

use your recently created user and password

User - nkp45
password - Replace **&amp;lt;password&amp;gt;** with the user's password ( you created ). Ensure any option params are [URL encoded](https://dochub.mongodb.org/core/atlas-url-encoding).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;create a file db.js inside configs&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;db.js&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
mongoose.set('strictQuery', false);
const connect = () =&amp;gt; {
  return mongoose.connect(
    'mongodb+srv://nkp45:&amp;lt;password&amp;gt;@cluster0.buxffaj.mongodb.net/?retryWrites=true&amp;amp;w=majority',
    {
      useUnifiedTopology: true,
      useNewUrlParser: true
    }
  );
};

module.exports = connect;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;index.js&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const app = require('./server');
const connect = require('./src/configs/db');

const port = '8000';
app.listen(port, async () =&amp;gt; {
  try {
    await connect();
    console.log('This server runs at port ' + port);
  } catch (e) {
    console.log(e.message);
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;server.js&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config();
const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use(express.static('public'));

const userController = require('./src/controllers/userController');
app.use('/users', userController);

module.exports = app;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;create a file userModel inside models&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;userModel.js&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');

const userSchema = new mongoose.Schema(
  {
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    roles: [
      {
        type: String,
        required: true,
        default: 'customer',
        enum: ['customer', 'admin', 'both']
      }
    ]
  },
  {
    versionKey: false,
    timestamps: true
  }
);

userSchema.pre('save', function (next) {
  if (!this.isModified('password')) return next();
  this.password = bcrypt.hashSync(this.password, 8);
  next();
});

userSchema.methods.checkPassword = function (password) {
  return bcrypt.compareSync(password, this.password);
};

const User = mongoose.model('user', userSchema);
module.exports = User;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;create a file userController inside the controller folder&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;userController.js&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const router = express.Router();

const User = require('../model/userModel');

router.post('', async (req, res) =&amp;gt; {
  try {
    const user = await User.create(req.body);
    return res.status(201).send(user);
  } catch (e) {
    console.log(e.message);
    return res.status(500).send(e.message);
  }
});

router.get('', async (req, res) =&amp;gt; {
  try {
    const user = await User.find().lean().exec();
    return res.status(201).send(user);
  } catch (e) {
    console.log(e.message);
    return res.status(500).send(e.message);
  }
});

router.get('/:id', async (req, res) =&amp;gt; {
  try {
    const user = User.findById(req.params.id).lean().exec();
  } catch (e) {
    console.log(e.message);
    return res.status(500).send(e.message);
  }
});

module.exports = router;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;nodemon setup&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Nodemon - it is a tool that helps develop Node. js based applications by automatically restarting the node application when file changes in the directory are detected. nodemon does not require any additional changes to your code or method of development. nodemon is a replacement wrapper for node .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "nodemon index.js"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;add this to package.json&lt;/p&gt;

&lt;p&gt;That's all - &lt;strong&gt;nodemon start&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>openai</category>
      <category>chatgpt</category>
      <category>abotwrotethis</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
