0

I am new in angular and node js. I want to know how angular connect with node js with mysql server. Which simple return query result. Can anyone help me.

3
  • Do you have some code relating to this or? Commented May 16, 2018 at 7:08
  • investigate HttpClient in angular angular.io/guide/http Commented May 16, 2018 at 7:09
  • I haven't any sample code for post. I have read and created sample application in my local for learning by following angular official site by not able to get connect with anularjs with nodejs and mysql to get simple one record from table Commented May 16, 2018 at 7:21

2 Answers 2

1

Angular is a fronend framework and nodejs can be used to implement a backend for a system. And you can use mysql as your DBMS.

You have to implement your backend and frontend separately. From backend you are exposing endpoints, routes, apis to the external applications.

And you can access those apis,routes from angular using HttpClient module. You can make Http requests using that.

Hope this helps

Sign up to request clarification or add additional context in comments.

4 Comments

I have made most of the things by using Httpclient.
Can't say anything without seeing your code.. Please do share your code.
1) server.js server.js app.get("/getUsers",function(req,res){ connection.query('SELECT * from users', function(err, rows, fields) { if (!err){ return (JSON.stringify(rows)); }else{ console.log('Err'); } connection.end(); }); }); var port = 4711; app.listen( port, function() { console.log( 'working', port, app.settings.env ); }); ---------------------- getUser(): Observable <any>{ const url = "localhost:4711/getUsers"; this.http.get(url).subscribe((res)=>{ this.users= res; });
Does this get Http call works on a normal browser? what happens when you go to localhost:4711/getUsers in a browser? Dis you test the pai with Postman or other application?
1

You may need to use some libraries to make a connection between angular frontend and backend with MySQL database. You will need the express.js to handle the backend for the data request. Because you use the MySQL database, the database language would be different from any others such as MongoDB. The express provided database integration for the different databases. You also need a body-parser as a middleware to parse the request body. This is a very important part of your project. The req is very complicated and this middleware can help to get the information which you need. Here is a sample of how to use express connect mysql.

var express = require('express');
var query = require('./query')
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = express();
//Middleware for bodyparsing using both json and urlencoding
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});
//login
app.post('/login',(req,res)=>{
    var opts = req.body;
    query(" SELECT *FROM `v_users` WHERE userAcount = ?",opts.userName).then((result)=>{
        var response = result[0];
        if(opts.password !== response.u_password){
            return res.send({
                errorCode:'404',
                errorMsg:'password error'
            })
        }
        //loginToken
        var loginToken = response.userAcount + Math.random()*Math.pow(10,16)
        res.send({
            loginToken:loginToken
        })
    })
})
var server = app.listen(3000,()=>{
    console.log('success')
})

Here is the query method:

(function() {
    var mysql = require('mysql');
    // var session = require('cookie-session');
    var query = (sql,key) => {
        var connection = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'root123',
            database: 'm_users'
        });
        connection.connect()
        var promise = new Promise((resolve,reject)=>{
            connection.query(sql,[key], function(error, results, fields) {
                if(error){
                    reject(error)
                }else{
                    resolve(results);
                }
            });
            connection.end();
        });
        return promise;
    }
    module.exports = query;
})()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.