简体   繁体   English

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

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

I am new in meteor js and web app is created in meteor. 我是meteor js的新手,在meteor中创建了web app。 I need to create API's for mobile app and native and web app will share the same database. 我需要为移动应用程序创建API,本机和Web应用程序将共享相同的数据库。 This is not clear to me from where I need to start to create API for the native app? 我不清楚从哪里开始为本机应用程序创建API? This is my login route which I am using for the web app. 这是我用于网络应用程序的登录路线。 Path of web app login route Web app登录路径的路径

 socialapp\socialappv1\app\lib\routes.js

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

and to create API I have created a server.js file in socialapp\\socialappv1\\app\\server\\ directory and I am trying to create API to register a user. 并创建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");
 }


});   

Is there any other way to create rest API eg to call controllers or is this correct to start API? 有没有其他方法来创建rest API,例如调用控制器或启动API是否正确?

I think your code may be trying to set up client side routes (not sure which router you are using). 我认为您的代码可能正在尝试设置客户端路由(不确定您使用的路由器)。

You need to add server side routes (and you can use express for this), and the handler needs to attach to the Meteor environment 您需要添加服务器端路由(并且您可以使用express),并且处理程序需要附加到Meteor环境

This is some code I have written to handle payment confirmations coming to the server: (server side code of course) 这是我为处理进入服务器的付款确认而编写的一些代码:(服务器端代码当然)

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