Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express')
- var app = express()
- var bodyParser = require ('body-parser')
- var colors = require('colors');
- const mysql = require('mysql');
- app.use(bodyParser.json());
- var knex = require('knex')({
- client: 'mysql',
- connection: {
- }
- });
- const connection = mysql.createConnection({
- host : 'localhost',
- user : 'pma',
- password : '123',
- database : 'schooldiary'
- });
- connection.connect(function(err) {
- if(err) console.log(err.red);
- else console.log(colors.green("Connected to DB."));
- });
- function register(json, cb){
- var knexb = knex('users').insert({login: json['login'],
- password: json['password'],
- email: json['email'],
- phone: json['phone'],
- canupdate: 0}).returning('*').toString();
- var jres = {};
- connection.query(knexb, function (error,results,fields){
- if(error) {
- if ((error.toString()).search("Login")>-1)
- jres['error'] = 'Login already exist';
- if ((error.toString()).search("Email")>-1)
- jres['error'] = 'Email already exist';
- if ((error.toString()).search("Phone")>-1)
- jres['error'] = 'Phone already exist';
- } else{
- jres['error'] = 'OK';
- console.log(("New User: "+json['login'] + " ;Password: " + json['password'] + " ;Email: " + json['email'] + " ;Phone: " + json['phone']).blue);
- }
- cb(jres);
- });
- }
- function isLogin(json,cb){
- var jres = {};
- var knexb = knex('users')
- .where({
- login:json['login']
- }).select('password').toString();
- // console.log(knexb);
- connection.query(knexb, function (error,results,fields){
- if(error) {
- json ['error'] = error.code;
- console.log(("ERRRRRRRRROR for user:" + json['login'] + " TEXTTTT: " + error.code).red);
- }
- else{
- if (results.length==1) {
- if (json['password'].toString() === results[0].password.toString()) {
- jres['error'] = 'OK';
- console.log(("Correct Password for user: " + json['login']).blue);
- }
- else {
- jres['error'] = "password isn't correct";
- console.log(("Password isn't correct for user: " + json['login']).cyan);
- }
- }
- else{
- jres['error'] = "user does not exist"
- }
- }
- cb(jres);
- });
- }
- app.post('/register', function(request, response){
- // your JSON
- //isLogin(request.body);
- register(request.body, function(json){
- response.json(json);
- });
- });
- app.post('/login',function(request,response){
- isLogin(request.body,function(json){
- response.json(json);
- })
- })
- app.get('/', function (req, res) {
- console.log(('CONNECTED MOBILE PHONE').magenta);
- res.send('Hello World!')
- })
- app.listen(3000, function () {
- console.log(('Example app listening on port 3000!').magenta)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement