这是什么问题?如何解决这个问题。其中一个应用程序运行良好。但我正面对这个问题与另一个api,但我不知道我在这里面临的问题是什么?
任何人帮助我?
这是我的代码:
var
express = require('express'),
path = require('path'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
port = process.env.PORT || 8080,
mongoose = require('mongoose'),
openApi = require('./app/api/openApi'),
familyApi = require('./app/api/familyApi'),
app = express();
//app config;-
var base = app.use( express.static( __dirname + '/public') );
var reqest = app.use( morgan('dev') );
var jsonParse = app.use( bodyParser.json() );
var urlParse = app.use( bodyParser.urlencoded({extended:true}));
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization');
next();
});
// mongoose.connect('mongodb://node:noder@ds013456.mlab.com:13456/practical');
mongoose.connect( 'mongodb://3gwebtrain:root@ds021166.mlab.com:21166/students', function( err ) {
if( err ) console.log( err );
console.log( 'connection successful!' );
});
//calling api from another files;
openApi( express, app );
// familyApi( express, app );
app.get('/', function( req, res ) {
res.sendFile( path.join( __dirname + '/public/app/views/index.html') );
});
app.listen( port );
console.log('app is listening at', port );
我打开的api文件:
"use strict";
var User = require('../models/userModel');
module.exports = function( express, app ){
var openApi = express.Router();
openApi.post('/create', function( req, res ){
var user = new User();
user.name = req.body.name;
user.username = req.body.username;
user.password = req.body.password;
console.log( user.name, user.username, user.password + '\n' + user );
user.save(function(err) {
if (err) {
// duplicate entry
if (err.code == 11000)
return res.json({ success: false, message: 'A user with that username already exists. '});
else
return res.send(err);
}
res.json({ message: 'User created!' });
});
});
//prefixing the open apis
app.use('/open', openApi);
return openApi;
}
答案 0 :(得分:0)
在邮递员中尝试这个
localhost:8080/create