繁体   English   中英

Meteor JS如何为本机app创建api?

[英]Meteor JS how to create api for native app?

我是meteor js的新手,在meteor中创建了web app。 我需要为移动应用程序创建API,本机和Web应用程序将共享相同的数据库。 我不清楚从哪里开始为本机应用程序创建API? 这是我用于网络应用程序的登录路线。 Web app登录路径的路径

 socialapp\socialappv1\app\lib\routes.js

 Router.route('login', {
   name: 'login',
   controller: 'LoginController',
   where: 'client'
 });

并创建API我在socialapp \\ socialappv1 \\ app \\ server \\目录中创建了一个server.js文件,我正在尝试创建API来注册用户。

Router.route('/register/',{where: 'server'})

.post(function(){
//console.log(this.request.body);
//return false;
let user = { 
email : this.request.body.email,
username : this.request.body.username,
password : this.request.body.password,

};

});
 const userId = Accounts.createUser(user);
 if(userId)
 {

    console.log("Register");
 }else{

    console.log("Not Register");
 }


});   

有没有其他方法来创建rest API,例如调用控制器或启动API是否正确?

我认为您的代码可能正在尝试设置客户端路由(不确定您使用的路由器)。

您需要添加服务器端路由(并且您可以使用express),并且处理程序需要附加到Meteor环境

这是我为处理进入服务器的付款确认而编写的一些代码:(服务器端代码当然)

import { Meteor } from 'meteor/meteor'
import express from 'express'
import bodyParser from 'body-parser'

const debug = require('debug')('b2b:server-payments')

async function acceptPayment(req, res) {
  // We need to bind to the Meteor environment for this to work.
  Meteor.bindEnvironment(() => {
    debug('/payment hook', req.body)
    try {
// Handle the data that's in req.body ...
  : 
  :
    } catch (e) {
      console.error(e)
    }
  })()

  res.status(200).json({ status: 'ok' }) // Change this if your data isn't JSON
}

export function setupPaymentsApi() {
  debug('Setting up payment hooks')
  const app = express()
  app.use(bodyParser.json({ extended: false }))

  app.post('/payment', acceptPayment)
  app.get('/api', (req, res) => {
    res.status(200).json({ message: 'B2B Payments API' }) // Shouldn't call this, just for testing for now
  })

  WebApp.connectHandlers.use(app)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM