我经历了一些stackoverflow问题。仍然无法在我的概念错误的地方。背景:nodejs app我正在学习,因为我想要它。它可以从CURL发出GET或POST请求,将数据填入mongodb。我的应用程序也在监听套接字。当ws.on('message')...我想在内部调用相同的Web API路由到我的应用程序。但这似乎没有引用。 “最终目标是获取套接字消息和GET / POST到我的内部/ api路由。下面我有我的应用程序代码:
server.js
var express = require('express');
var http = ('http'); const mongoose = ('mongoose');
const WebSocket = ('ws');
var routes = require('./routes/index'); var api = ('./routes/api');
var app = express();
app.use('/', routes);
app.use('/api', api);
mongoose.Promise = global.Promise;
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
} );
// connect to database
mongoose.connect('mongodb://localhost/mdb')
.then(() => console.log('db connection successful'))
.catch((err) => console.error(err));
http.createServer(app).listen(8080);
//Create a websocket to receive notification
const ws = new WebSocket('ws://localhost:8031');
ws.on('open', function open() {
console.log('ws connection successful');
});
//get or post message to webapi /api/v1 path
ws.on('message', function incoming(data) {
//this route seems never been called
app.get('api/v1', function (res) {
res.send('hello');
});
console.log('Socket message: ' + data);
});
module.exports = app;
api.js
var express = require('express');
var router = express.Router();
const mogoose = require('mongoose');
const Data = require('../models/dbModel');
const TempData = require('../models/TempModel');
/* GET data for listing. */
router.get('/v1/', function (req, res, next) {
Data.find(function (err, data)
{
if (err) return next(err);
res.json(data);
});
});
的package.json
"dependencies": {
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"debug": "~2.0.0",
"express": "^4.9.8",
"express-controller": "^0.3.2",
"jade": "~1.6.0",
"mongodb": "^3.0.1",
"mongoose": "^5.0.2",
"morgan": "~1.3.0",
"request": "^2.83.0",
"serve-favicon": "~2.1.3",
"stylus": "0.42.3",
"ws": "^4.0.0"
},
"main": "server.js"
请填写您的意见和提示。已经坚持这个问题1.5天了。
答案 0 :(得分:1)
您可以使用supertest
并拨打自己的API。
const app = require('./server.js');
const request = require('supertest');
request(app)
.get('/api/v1')
.expect(200)
.end(function(err, res) {
if (err) throw err;
else console.log(res);
});
参考:https://github.com/visionmedia/supertest
你也可以创建一个这样的函数:
router.get('/v1/', function (req, res, next) {
exports.findData(function(results){
res.json(results);
})
});
exports.findData = function(callback) {
Data.find(function (err, data) {
if (err) return next(err);
callback(data);
});
}
在您的WebSockets中:
const findData = require('./router').findData;
// ...
ws.on('message', function incoming(data) {
findData(function () {
console.log('Socket message: ' + data);
})
});
答案 1 :(得分:0)
const WebSocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path');
ws.on('open', function open() {
ws.send('something');
});
ws.on('message', function incoming(data) {
console.log(data);
});
这似乎是配置问题,请参阅:const ws = new WebSocket('ws://www.host.com/path');
答案 2 :(得分:0)
为什么要调用路由,只需对套接字事件执行相同的操作
// api.js
var handler = function (cb) {
// Do some stuff
Data.find(cb);
}
router.get('/v1/', (req, res, next) => {
handler(function (err, data) {
if (err) return next(err);
res.json(data);
});
});
module.exports = {
handler
};
// server.js
var appJs = require('./app');
ws.on('message', function incoming(data) {
/* No need to call route perform whatever operation route is doing */
appJs.handler(function (err, data) {
console.log('Socket message: ' + data);
// Do some stuff
});
});