节点,模块使用结构

时间:2017-09-24 10:39:39

标签: javascript angularjs node.js express stripe-payments

我一直在努力了解如何为我的应用程序设置Stripe,但是我遇到了模块实现方面的问题。通常在使用模块时我会要求它在文件的顶部才能使用它,但我不知道如何在paymentController文件中执行此操作,或者我甚至需要。我导入了条纹npm,所以我的意思是我可以全局访问它吗?好吧,你看,我对此很新,并想了解如何构建这个,以便付款工作。

app.js文件:

angular.module('userApp', ['appRoutes', 'userControllers', 'userServices', 'ngAnimate', 'mainController', 'authServices', 'managementController', 'paymentController'])

.config(function($httpProvider) {
    $httpProvider.interceptors.push('AuthInterceptors');
});

paymentController文件:

angular.module('paymentController', [])

.controller('paymentCtrl', function($scope) {

  var app = this; 


});

Server.js文件:

var express = require('express'); // ExperssJS Framework
var app = express(); // Invoke express to variable for use in application
var port = process.env.PORT || 8080; // Set default port or assign a port in enviornment
var morgan = require('morgan'); // Import Morgan Package
var mongoose = require('mongoose'); // HTTP request logger middleware for Node.js
var bodyParser = require('body-parser'); // Node.js body parsing middleware. Parses incoming request bodies in a middleware before your handlers, available under req.body.
var router = express.Router(); // Invoke the Express Router
var appRoutes = require('./app/routes/api')(router); // Import the application end points/API
var path = require('path'); // Import path module
var passport = require('passport'); // Express-compatible authentication middleware for Node.js.
var social = require('./app/passport/passport')(app, passport); // Import passport.js End Points/API

app.use(morgan('dev')); // Morgan Middleware
app.use(bodyParser.json()); // Body-parser middleware
app.use(bodyParser.urlencoded({ extended: true })); // For parsing application/x-www-form-urlencoded
app.use(express.static(__dirname + '/public')); // Allow front end to access public folder
app.use('/api', appRoutes); // Assign name to end points (e.g., '/api/management/', '/api/users' ,etc. )


mongoose.connect('mongodb://localhost:27017/tutorial', function(err) {
  if (err) {
    console.log('Not connected to the database: ' + err);
  } else {
    console.log('Successfully connected to MongoDB');
  }
});

// Set Application Static Layout
app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/app/views/index.html')); // Set index.html as layout
});

// Start Server
app.listen(port, function() {
    console.log('Running the server on port ' + port); // Listen on configured port
});

1 个答案:

答案 0 :(得分:0)

我建议您遵循此Stripe教程中针对Node显示的内容:

https://stripe.com/docs/charges

就像你的其他包含一样,你想在任何将使用Stripe库的JS文件的顶部放置这样的东西:

var stripe = require('stripe')('sk_my_secret_key')

然后,在同一文件的其他位置,您可以调用所需的任何Stripe库方法:

stripe.charges.create(…)

当然,在制作中你要构建一个合适的12 Factor app [1]并将你的秘密放在环境变量或配置文件中。

[1] https://12factor.net/config