Как мы сейчас общаемся с сервером?

  • самый популярный способ, REST API с выборкой или axios
fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));
  • или GraphQL
const GET_ALL_TODOS = gql`
  query GetAllTodos {
    todos {
      edges {
        node {
	  completed
	  id
	  text
	}
      }
    }
`;

export const Todos = () => {
  const { loading, error, data } = useQuery(GET_ALL_TODOS);
  ...
}
  • или решения в реальном времени, веб-сокет, SSE
io.on('connection', (socket) => {
  socket.on('chat message', (msg) => {
    console.log('message: ' + msg);
  });
  socket.emit('chat message', input.value);
});
  • другие, xml/protobuff….
axios.get('/api/messages', {responseType: 'arraybuffer'})    
  .then(function (response) {
    console.log('Response from the server: ', response)    
    let msg = Message.decode(response.data)    
    console.log('Decoded message', msg)
  })

Решение Fullstack приходит на помощь!

они упрощают использование серверных данных

  • метеор, вызывайте коллекции монго, похожие на монго апи
import React from 'react';
import { useTracker } from 'meteor/react-meteor-data';
import { TasksCollection } from '/imports/api/TasksCollection';
import { Task } from './Task';
 
export const App = () => {
  const tasks = useTracker(() => TasksCollection.find({}).fetch());
 
  return (
    <div>
      <h1>Welcome to Meteor!</h1>
      <ul>
        { tasks.map(task => <Task key={ task._id } task={ task }/>) }
      </ul>
    </div>
  );
};

ember.js, автоматическое сопоставление данных модели с шаблоном

import Route from '@ember/routing/route';

export default class ScientistsRoute extends Route {
  model() {
    return ['Marie Curie', 'Mae Jemison', 'Albert Hofmann'];
  }
}
---
<h2>List of Scientists</h2>
<ul>
  {{#each @model as |scientist|}}
    <li>{{scientist}}</li>
  {{/each}}
</ul>
  • есть также решения, подобные firebase/rethink, я действительно люблю firebase, но дело в том, что его нельзя разместить на вашем собственном сервере.
var ratingRef = firebase.database().ref("ratings/");
ratingRef.orderByValue().on("value", function(data) {   
   data.forEach(function(data) {
      console.log("The " + data.key + " rating is " + data.val());
   });   
});

Наконец, почему бы не вызывать внутренние функции напрямую?

нравится

import { Cat } from '../common/Cat.mjs'
export const hello = async name => {
  let cat = await Cat.findOne({})
  return `hello ${name}! from ${cat.name}`
}
----
import { hello } from "../apis/hello.mjs";
(async () => alert(await hello('world')))()

да, теперь вы можете, с shack.js