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

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

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);

求大神解答

黄舟
黄舟

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

Antworte allen(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 
})
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage