Node Js + MySql + Express – app.use(bodyParser()); 没有定义 – 错误build立连接到新的SQL方法

我试图映射连接基于路由器文件的数据库中的注册信息,但我没有得到通过路由器文件返回;

菜单导航>转到页面>获取信息到“efetuar venda”>在表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 from "./config/config"; import mongodb from "./config/mongodb"; import passport from "./config/passport"; import * as setup from "./middleware/setup"; import app from "./config/express"; import routes from "./routes"; import User from './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发送数据到路由快递。

 $(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); } }); }); }); 

在sell.html中input表单

 <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 do 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 a 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> 

我能够通过index.js使用app.getredirect模板来稳定连接,但我想在不同的路由文件中使用连接mysql。

非常感谢您的帮助 :]