Nodejs中间件有两个文件

时间:2018-01-23 10:30:18

标签: javascript node.js express

您好我有这两个文件,并尝试从server.js调用index.js,它工作正常,并在代码的另一侧打印,但它不在<% if Quantity %> <% if Quantity == 1 %> <option value="1">1</option> <% end_if %> <% if Quantity == 2 %> <option value="1">1</option> <option value="2">2</option> <% end_if %> <% if Quantity == 3 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <% end_if %> <% if Quantity == 4 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <% end_if %> <% if Quantity == 5 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <% end_if %> <% if Quantity == 6 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <% end_if %> <% if Quantity == 7 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <% end_if %> <% if Quantity == 8 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <% end_if %> <% if Quantity == 9 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <% end_if %> <% if Quantity > 9 %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <% end_if %> <% else %> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <% end_if %> 函数内。请告知我的错误

Server.js

app.get

index.js

const express = require("express")

const bodyParser = require("body-parser")

const app = express()

var routes = require('./api/index.js')

const PORT = process.env.PORT || 3000

app.use(bodyParser.urlencoded({ extended: true }))

app.use(bodyParser.json())

app.use('/t', require('./api/index')(app))

var server = app.listen(PORT, function (){
    var host = server.address().address
    var port = server.address().port

    console.log('Middle ware started on address http://%s:%s',host,port)
})

3 个答案:

答案 0 :(得分:1)

你的方法是不正确的尝试

<强> server.js

const express = require("express")

const bodyParser = require("body-parser")

const app = express();

const PORT = process.env.PORT || 3000

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

app.use('/t', require('./api/index.js'));

var server = app.listen(PORT, function (){
    var host = server.address().address
    var port = server.address().port

    console.log('Middle ware started on address http://%s:%s',host,port)
})

<强> index.js

const express = require('express');
router=express.Router();
router.get('/',function(req,res){
      console.log('we are there')
       console.log(res)
       res.send('success')

});
module.exports=router;

答案 1 :(得分:1)

您应该使用快速Router class来创建模块化路由处理程序:

<强> Server.js

const express = require("express")

const bodyParser = require("body-parser")

const app = express()

var routes = require('./api/index.js')

const PORT = process.env.PORT || 3000

app.use(bodyParser.urlencoded({ extended: true }))

app.use(bodyParser.json())

app.use('/t', routes)

app.listen(PORT, function (){
    var host = server.address().address
    var port = server.address().port

    console.log('Middle ware started on address http://%s:%s',host,port)
})

<强> index.js

const express = require('express')
const router = express.Router()

router.get('/',function(req,res){
    console.log('we are there')
    res.send('successfully get /t')
})

module.exports = router;

答案 2 :(得分:0)

<强> Server.js

const express = require("express") 
const bodyParser = require("body-parser") 
const app = express()

var routes = require('./api/index.js')(app)  // Pass the app

const PORT = process.env.PORT || 3000

app.use(bodyParser.urlencoded({ extended: true }))

app.use(bodyParser.json())

app.use('/t', routes)  // change this to routes

var server = app.listen(PORT, function (){  var host = server.address().address  var port = server.address().port

console.log('Middle ware started on address http://%s:%s',host,port) })

index.js

  const express = require('express')
  module.exports = function(app) {  
    console.log('we are in other part of code') 
    app.get('/',function(req,res){  
      console.log('we are there') 
      console.log(res)
      res.json({success : true});
    })
  }