Getting started with MEAN (Mysql, Express, Angular 6, Node) Part-3

This session, I am explaining about node server creation and table operations from MySQL database using sequelize models.

First, initialize Node using below command and fill the required details.

npm init

npm intilization.png

now create index.js (mentioned in entry point) file and paste below code.

//======== Import Required Files ===============
var express = require(‘express’);
var bodyparser = require(‘body-parser’);
var cors = require(‘cors’);
var jwt = require(‘jsonwebtoken’);
//============ Create Express app and initialize to app variable ========
var app = express();
// ============= initialize cros ==============
app.use(cors());
//=========== set port ================
app.set(‘port’,process.env.port || 3000 );
//========= initialize body parser ==========
app.use(bodyparser.json());
//=========== Start Code Here ====================
app.get(‘/’,(req,res)=>{
    res.send({‘status’:true,’msg’:”Server Started.”});
});
//============== Code Ends Here ===================
//====== Start Server ===================
app.listen(app.get(‘port’),()=>{
    console.log(‘Server Created in ‘+app.get(‘port’)+”Port”);
});
Run node server using below command and see your results in http://localhost:3000/
node index.js
node server.png

Insert some dummy data in your database tables. Then get the inserted data using sequelize.

if we want to get the data first include the users model.

var user = require(‘./models’).user;
var roles = require(‘./models’).roles;

Then place the below code and get output in http://localhost:3000/get-users. More details regarding models, go through this link (Click Here)

app.get(‘/get-users’,(req,res)=>{
   user.findAll({ include: [{model:roles} ] }).then(users=> {
       res.send({‘status’:true,’data’:users});
   });
});

So you can insert user using below code(using postman you can add user details).

app.post(‘/create-user’,(req,res)=>{
   var user_data=req.body;
   user.create({
   name:user_data.name,
   email:user_data.email,
   password:user_data.password,
   phone:user_data.phone,
   roleId:user_data.roleId
   }).then((users)=>{
      res.send({‘status’:true,’msg’:users});
   });
});
Postman.PNG

Thank you, guys. In the next session, I will explain the connections between angular 6 and node.

Git Links:

Front End:  https://github.com/iamchandu/angular6-signup-login-frontend

Backendhttps://github.com/iamchandu/SampleREST-APIsUsing-Node-JWT-Sequelize

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
<span>%d</span> bloggers like this: