node.js - node启动服务器以后显示正常运行浏览器却打不开。
黄舟
黄舟 2017-04-17 15:19:02
0
1
696

我也不说啥了,直接上代码吧!

var express = require('express'),
    //session = require('express-session'),
    mongoose = require('mongoose'),
    router = require('./lib/router'),
    bodyParser = require('body-parser'),
    multer = require('multer'),
    User = require('./lib/models/user'),
    app = express();



app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // for parsing application/json
app.use(multer);


app.use(express.static(__dirname + '/'));

//app.use(bodyParser.json());

mongoose.connect('mongodb://localhost:27017/tfdb');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.on("open", function () {
    console.log("------数据库连接成功!------");
});

router(app);

//User.create({uid:123,name:'test','phone':'12313',password:'12312321'},function (err,data) {
//    if(err)
//        console.log(err);
//
//
//});

console.log('you have  logged in  successfully!');

app.listen(5000);

求大神解答

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回覆(1)
洪涛
app.use(multer); //此处错误

這樣寫會讓multer把所有請求當做文件上傳進行攔截,所以瀏覽器看不到回應

正確用法是綁定到特定請求,如下

let upload = multer({ dest: 'uploads/' })
app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file 
  // req.body will hold the text fields, if there were any 
})
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板