2017-08-16 75 views
0

我有以下文件:(auth.js快遞不能到達的路線

module.exports = function (express, mongoose, router) { 

    router.route('/setup') 
     .get(function (req, res) { 
      var salt = bcrypt.genSaltSync(10); 
      var hashed = bcrypt.hashSync(req.query.password, salt); 
      // create a sample user 
      mongoose.models.playlist.create({username: 'Hieratic', password: hashed}, function (err, item) { 

       if (err) throw err; 

       console.log('User saved successfully'); 
       res.json({ success: true }); 
      }); 

     }); 


    return router; 
}; 

而且目前的服務器文件:(server.js

"use strict"; 

var url = require('url'); 
var express = require('express'); 
var bodyParser = require('body-parser'); 
var fs = require('fs'); 
var crypto = require('crypto'); 
//npm install request 
var request = require('request'); 
var config = require('./config/access.json'); 
var admin = require("firebase-admin"); 
var fireBaseConfig = require('./config/firebase.config.json'); 
var mongoose = require('mongoose'); 
var MongoClient = require('mongodb').MongoClient; 
var mongodbConfig = require('./config/database.json'); 
var exec = require('child-process-promise').exec; 
var app = express(); 

/** 
* Schemas 
*/ 
var UserModel = require('./schemas/user.schema.js')(mongoose); 
var PlayListModel = require('./schemas/playlist.schema.js')(mongoose); 


app.use(function (req, res, next) { 
    // CORS headers 
    res.header('Access-Control-Allow-Origin', '*'); 
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); 
    res.header("Access-Control-Allow-Headers", 
     "Origin, X-Requested-With, Content-Type, Accept, x-access-token, Cache-Control, Pragma" 
    ); 
    next(); 
}); 
var router = express.Router(); 
app.use(bodyParser.json({limit: '4mb'})); 
app.use(bodyParser.urlencoded({ 
    extended: true, 
    limit: '4mb' 
})); 

if (mongodbConfig.username && mongodbConfig.password) { 
    mongoose.connect(`mongodb://${mongodbConfig.username}:${mongodbConfig.password}@${mongodbConfig.host}:${mongodbConfig.port}/${mongodbConfig.database}`); 
} 
else { 
    mongoose.connect(`mongodb://${mongodbConfig.host}:${mongodbConfig.port}/${mongodbConfig.database}`); 
} 
process.on('disconnect', function() { 
    // If mongodb connection is not terminated child process never dies 
    mongoose.disconnect(); 
}); 
require('./auth.js')(express, mongoose, router); 


var port = 51000; 
app.listen(port); 
console.log('Magic happens at http://localhost:' + port); 

當我試圖訪問/setup其路由確實打不過服務器,它拋出一個HTML頁面:

http://localhost:51000/setup):

<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Error</title> 
</head> 
<body> 
    <pre>Cannot GET /setup</pre> 
</body> 

任何人都可以看到香港專業教育學院做錯了?

回答

3

您已經創建router,但不能把它註冊爲一箇中間件:

var router = express.Router(); 
require('./auth.js')(express, mongoose, router); 
app.use(router); 
+0

THS工作哇,我真是太愚蠢:) –

0

參考Cannot GET/Nodejs Error 你沒有路線設置。 服務您的網頁是這樣的:

app.get('/', function (req, res) { 
    res.render('index', {}); 
}); 
+0

我不想一個網頁,我只是想訪問的路線。 –