admin管理员组文章数量:1192947
I'm new to node.js but I know somewhat about socketstream web framework by using this I can easily call a server side node.js method from JavaScript. I don't know how to do this without using that framework. How can I call the node.js method from JavaScript?
The below code is using socketstream to call server side method. So I want to call the same server side method without using this framework.
ss.rpc('FileName.methodName',function(res){
alert(res);
});
I'm new to node.js but I know somewhat about socketstream web framework by using this I can easily call a server side node.js method from JavaScript. I don't know how to do this without using that framework. How can I call the node.js method from JavaScript?
The below code is using socketstream to call server side method. So I want to call the same server side method without using this framework.
ss.rpc('FileName.methodName',function(res){
alert(res);
});
Share
Improve this question
edited Feb 19, 2013 at 6:40
mjgpy3
8,9375 gold badges33 silver badges53 bronze badges
asked Feb 19, 2013 at 6:33
user1629448user1629448
2271 gold badge4 silver badges10 bronze badges
3
|
2 Answers
Reset to default 18I'd suggest use Socket.IO
Server-side code
var io = require('socket.io').listen(80); // initiate socket.io server
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' }); // Send data to client
// wait for the event raised by the client
socket.on('my other event', function (data) {
console.log(data);
});
});
and client-side
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost'); // connec to server
socket.on('news', function (data) { // listen to news event raised by the server
console.log(data);
socket.emit('my other event', { my: 'data' }); // raise an event on the server
});
</script>
Alternatively, you can use a router function which calls some function on specific request from the client
var server = connect()
.use(function (req, res, next) {
var query;
var url_parts = url.parse(req.url, true);
query = url_parts.query;
if (req.method == 'GET') {
switch (url_parts.pathname) {
case '/somepath':
// do something
call_some_fn()
res.end();
break;
}
}
})
.listen(8080);
And fire AJAX
request using JQuery
$.ajax({
type: 'get',
url: '/somepath',
success: function (data) {
// use data
}
})
Not exaclty sockets but a simple solution:
Can I suggest trying api-mount. It basically allows calling API as simple functions without having to think about AJAX requests, fetch, express, etc. Basically in server you do:
const ApiMount = apiMountFactory()
ApiMount.exposeApi(api)
"api" is basically an object of methods/functions that you are willing to call from your web application.
On the web application you then do this:
const api = mountApi({baseUrl: 'http://your-server.com:3000'})
Having done that you can call your API simply like this:
const result = await api.yourApiMethod()
Try it out. Hope it helps.
本文标签: How to call nodejs server side method from javascriptStack Overflow
版权声明:本文标题:How to call node.js server side method from javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738438101a2086796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
app.get('/some.name', function(req, res) { // call code })
. Then you can hit that endpoint via an AJAX call on the client. – jli Commented Feb 19, 2013 at 6:46