我是node.js的新手。我做了这样的路线/索引,我不知道这是好还是不好?
我的代码运行完美,但是:
1-谁能解释这个代码标准吗?
2-我可以为控制器使用外部文件夹/文件吗?像:
控制器/用户/index.js
控制器/api/index.js
routes / index.js
var express = require("express")
var router = express.Router()
var request = require("request");
let index = {
// ## Controls
getBTC: function () {
request.get("http://api.coindesk.com/v1/bpi/currentprice.json", (err, response, body) => {
if (err) {
return console.dir(err);
}
rate = JSON.parse(body).bpi.USD.rate
time = JSON.parse(body).time.updated
})
},
// ## Routes
indexRoute: function () {
router.get('/', function (req, res, next) {
res.render('common/index', {
title: 'Index Title',
rate: this.rate,
time: this.time
})
})
},
testRoute: function () {
router.get('/test', function (req, res, next) {
res.render('common/test', {
title: 'Test Title',
rate: this.rate
})
})
}
}
index.getBTC() //-> Enable For All Routes
index.indexRoute()
index.testRoute()
module.exports = router
views / common / index.pug
extends ../layout
block content
p #{rate}
h1 #{time}