Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = require('express')();
- var http = require('http').Server(app);
- var mysql = require('mysql');
- var bodyParser = require("body-parser");
- var connection = mysql.createConnection({
- host : 'localhost',
- user : 'root',
- password : '',
- database : 'larachat',
- });
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(bodyParser.json());
- app.get('/',function(req,res){
- var data = {
- "Data":""
- };
- data["Data"] = "Welcome to type Store DEMO...";
- res.json(data);
- });
- app.get('/chat',function(req,res){
- var data = {
- "error":1,
- "Type":""
- };
- connection.query("SELECT * from chat",function(err, rows, fields){
- if(rows.length != 0){
- data["error"] = 0;
- data["Types"] = rows;
- res.json(data);
- }else{
- data["Types"] = 'No types Found..';
- res.json(data);
- }
- });
- });
- app.post('/chat/create',function (req, res) {
- var Chat = req.body.chat;
- var User_id = req.body.user_id;
- var data = {
- "error":1,
- "types":"",
- };
- if(!!Chat && !!User_id){
- connection.query("INSERT INTO chat VALUES('',?,?,)",[Chat,User_id],function(err, rows, fields){
- if(!!err){
- data["types"] = "Error Adding data";
- }else{
- data["error"] = 0;
- data["types"] = "type Added Successfully";
- }
- res.json(data);
- });
- }else{
- data["types"] = "Please provide all required data (i.e : chat, user_id)";
- res.json(data);
- }
- });
- http.listen(8080,function(){
- console.log("Connected & Listen to port 8080");
- });
Advertisement
Add Comment
Please, Sign In to add comment