javascript - Node Js + MySql + Express - app.use(bodyParser()); is not defined - Error to establish connection to new-sql method -
i try mapping connection based on router file register information in data base did not return via router file;
menu-navigation > go page > informations "efetuar venda" > post in table sql
index.js
var http = require('http'); var express = require('express'); var mysql = require('mysql'); var bodyparser = require('body-parser'); var methodoverride = require('method-override'); var errorhandler = require('errorhandler'); import config "./config/config"; import mongodb "./config/mongodb"; import passport "./config/passport"; import * setup "./middleware/setup"; import app "./config/express"; import routes "./routes"; import user './models/user'; export function start() { return new promise((resolve, reject) => { mongodb().then((db) => { let server = http.createserver(app); // save references app.db = db; app.server = server; app.config = config; // init passport passport(app); // register setup check middleware app.use(setup.check); // setup routes routes(app); // start server app.server.listen(config.server.port, (err) => { if (err) { return reject(err); } resolve(app); }); }, reject); }); }; var router = express.router(); var path = __dirname + '/routes/product/'; router.use(function (req,res,next) { console.log("/" + req.method); next(); }); router.get("/templates",function(req,res){ res.sendfile(path + "index.html"); }); router.get("/sell",function(req,res){ res.sendfile(path + "sell.html"); var connection = require('./routes/product/auth'); }); app.use("/",router);
auth.js
var express = require('express'); var mysql = require('mysql'); var bodyparser = require('body-parser'); var methodoverride = require('method-override'); var errorhandler = require('errorhandler'); var connection = mysql.createconnection({ host: 'localhost', user: 'root', password: '558595', database: 'famagas_itapetininga' }); connection.query('create database if not exists famagas_itapetininga', function (err) { if (err) throw err; connection.query('use famagas_itapetininga', function (err) { if (err) throw err; connection.query('create table if not exists new_sell(' + 'id int not null auto_increment,' + 'primary key(id),' + 'nameclient varchar(100),' + 'namefantasysell varchar(100),' + 'addresofclientsell varchar(10030),' + 'annotations text(500),' + 'neighborhood varchar(100),' + 'cep varchar(10),' + 'phonelandline varchar(100),' + 'cellphone varchar(100),' + 'autocompletebusinessreseller varchar(10030),' + 'amountproduct varchar(1000),' + 'producfinalprice varchar(100)' + ')', function (err) { if (err) throw err; }); }); }); // body request element app.use(bodyparser()); connection.connect(function(err) { if (err) throw err console.log('sucess connect..') }) module.exports = connection;
sell.html - ajax send post data route express.
$(document).ready(function () { $('#publish-sell').click(function () { var payload = { nameclient: $('#nameclient').val(), namefantasysell: $('#namefantasysell').val(), addresofclientsell: $('#addresofclientsell').val(), annotations: $('#annotations').val(), neighborhood: $('#neighborhood').val(), cep: $('#cep').val(), phonelandline: $('#phonelandline').val(), cellphone: $('#cellphone').val(), autocompletebusinessreseller: $('#autocompletebusinessreseller').val(), amountproduct: $('#amountproduct').val(), producfinalprice: $('#producfinalprice').val() }; $.ajax({ url: "/sell-sucess", type: "post", contenttype: "application/json", processdata: false, data: json.stringify(payload), complete: function (data) { $('#output').html(data.responsetext); } }); }); });
form input in sell.html
<article class="col s12"> <form action="product/sell" method="post" class="product-add" role="form"> <div class="input-field col s6"> <i class="material-icons prefix">perm_identity</i> <input placeholder="..." id="nameclient" name="nameclient" type="text" class="validate"> <label for="nameclient">nome cliente:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">perm_identity</i> <input placeholder="..." id="namefantasysell" name="namefantasysell" type="text" class="validate"> <label for="namefantasysell">nome fantasia:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">markunread_mailbox</i> <input placeholder="não utilizar letras maiusculas e acentos" id="addresofclientsell" name="addresofclientsell" type="text" class="validate"> <label for="addresofclientsell">endereço:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">class</i> <input placeholder="..." id="annotations" name="annotations" type="text" class="validate"> <label for="annotations">anotações:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">location_on</i> <input placeholder="..." id="neighborhood" name="neighborhood" type="text" class="validate"> <label for="neighborhood">bairro:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">markunread_mailbox</i> <input placeholder="opcional" id="cep" name="cep" type="number" class="validate"> <label for="cep">cep:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">call</i> <input placeholder="..." id="phonelandline" name="phonelandline" type="number" class="validate"> <label for="phonelandline">telefone fixo:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">stay_primary_portrait</i> <input placeholder="opcional" id="cellphone" name="cellphone" type="number" class="validate"> <label for="cellphone">celular:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">work</i> <input placeholder="opcional" type="text" id="autocompletebusinessreseller" name="autocompletebusinessreseller" class="autocomplete"> <label for="autocompletebusinessreseller">empresa ou revendedor:</label> </div> <div class="input-field col s6"> <i class="material-icons prefix">receipt</i> <input placeholder="não utilizar letras ou acentos, apenas números" id="amountproduct" name="amountproduct" type="number" class="validate"> <label for="amountproduct">quatidade:</label> </div> <div class="col s6"> <label>produto vendido:</label> <select multiple> <option value="" disabled selected>selecione seu produto, múltiplas escolhas estão permitido</option> <option value="1">option 1</option> <option value="2">option 2</option> <option value="3">option 3</option> </select> </div> <div class="col s6"> <label>quem fez venda?</label> <select multiple> <option value="" disabled selected>selecione seu funcionário, portaria, revendedor e etc.</option> <option value="1">option 1</option> <option value="2">option 2</option> <option value="3">option 3</option> </select> </div> <div class="input-field col s6"> <i class="material-icons prefix">label</i> <input placeholder="ex: 2,500" id="producfinalprice" name="producfinalprice" type="number" class="validate"> <label for="producfinalprice">preço final:</label> </div> <div class="col s3 left-align publish-sell-align"> <button class="btn waves-effect waves-light indigo" id="publish-sell" type="submit"><i class="material-icons left">attach_money</i>efetuar venda</button> <p class="divisor-publish-sell">ou</p> </div> <div class="col s3 right-align publish-sell-align"> <button class="btn waves-effect waves-light indigo" id="draft-sell" type="submit"><i class="material-icons left">backup</i>salvar rascunho</button> </div> </form> </article>
i able stabilize connection via index.js redirecting template utilizing app.get wanted use connection mysql in different route files.
thanks lot :]
Comments
Post a Comment