node.js - 不工作 - 在 IIS (iisnode) 上部署 Node 应用程序 - 返回 403 错误

标签 node.js express iis deployment web-deployment

我有一个 Node/快速 API 项目。我正在尝试通过 Internet 信息服务 (IIS) 运行该项目。我当前在 IIS 中的结构 - 站点 -> 环境(网站) -> project1 | project2(此处为 Node 应用程序)。我将 project2 转换为具有环境应用程序池名称的应用程序。然后我创建应用程序。

我尝试访问我的网址,但返回 403 禁止错误。我已经给了它所有的凭据。不确定为什么会这样。

我一直在使用的一些资源 - video blog

网络配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>

    <!-- 
      By default IIS will block requests going to the bin directory for security reasons. 
      We need to disable this since that's where Express has put the application entry point. 
    -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin" />
        </hiddenSegments>
      </requestFiltering>
    </security>

    <handlers>
      <!-- Indicates that the www file is a node.js entry point -->
      <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
    </handlers>
    <!--<rewrite>
      <rules>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="app.js\/debug[\/]?" />
        </rule>


        <rule name="StaticContent">
          <action type="Rewrite" url="public{REQUEST_URI}"/>
        </rule>


        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="app.js"/>
        </rule>
      </rules>
    </rewrite> -->

    <rewrite>
      <rules>
        <rule name="sendToNode">
        <match url="/*" />
        <action type="Rewrite" url="app.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

应用程序.js

const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const cors = require('cors')
const compression = require('compression')
const helmet = require('helmet')
const expressSanitizer = require('express-sanitizer')
const jwt = require('jwt-simple');
const index = require('./routes/index')
const something1 = require('./routes/something1')
const something2 = require('./routes/something2')
const responseTime = require('response-time')

const app = express()
// const app = express.createServer()
const port = 3000

var corsOptions = {
    origin: 'http://localhost:8100',
    optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 
}



//added security
app.use(helmet())

// //set logger
// app.use(logger)

//cors options
app.use(cors(corsOptions))

//body parser middleware
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

// Mount express-sanitizer here
app.use(expressSanitizer()) // this line follows bodyParser() instantiations

//set static path
app.use(express.static(path.join(__dirname, 'client')))
// app.use(express.static(path.join(__dirname, '../../www')))

//Use response time
app.use(responseTime())

// set our default template engine to "ejs"
// which prevents the need for using file extensions
app.set('view engine', 'ejs')

//gzip compression
app.use(compression())

//add authorization request header
app.use((req, res, next) => {
    if(!req.headers.authorization){
        return res.status(403).json({ error: 'No credentials sent!'});
    }
    // try {
    //     let token = req.headers.authorization.split(' ')[1]
    //     var decoded = jwt.decode(token, 'your secret here');
    //     console.log(decoded, 'decoded')
    //   } catch (err) {
    //     console.log('err ', err.stack);
    //     return res.status(403).json({
    //       error: 'invalid token'
    //     });
    //   }
    next();
})

//set views for error and 404 pages
app.set('views', path.join(__dirname, 'views'))

app.use('/', index)
app.use('/something1/v1', something1)
app.use('/something2/v1', something2)

// app.listen(port, () => {
//     console.log('server started on port 3000')
// })

//run export NODE_ENV=production
//NODE_ENV=production node app.js

app.listen(process.env.port)

我已经安装了所有东西 - (rewrite/node/iisnode) 只是不知道如何设置它。谢谢

最佳答案

嗯,我认为您在 IIS 中设置网站的方式不太正确。

听起来你的 IIS 结构是这样的:

Sites
└─── environment
     └─── project1
     └─── project2
          |    app.js
          |    web.config
          |    ...

其中environment是一个网站,它下面的每个文件夹可能是一个已经转换为application的文件夹。

这似乎是人们常犯的一个错误,因为 hello world 教程作为应用程序在 Default Web Site 下运行(虽然我不确定为什么会这样,但我猜是因为它不会替换可通过本地主机上的端口 80 轻松访问的默认网站)。这不是设置新 IISNode 网站的最佳方式。

您应该做的是添加一个新站点(右键单击 Sites 并从上下文菜单中选择 Add Website)。填写必要的详细信息并将 Physical path: 指向您的 app.jsweb.config 所在的位置。

如果您正确设置了 web.config,IISNode 应该会处理其余部分。

祝你好运,如果我做出了任何错误的假设,请告诉我。

关于node.js - 不工作 - 在 IIS (iisnode) 上部署 Node 应用程序 - 返回 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49984735/

相关文章:

jquery - 将 jQuery 与 Jade 结合使用

c# - 如何从 C# 更改 IIS 中应用程序池的用户名/密码?

iis - 启动 Internet 信息服务时,mmc 无法创建错误的管理单元

Javascript 对象最大内存大小

javascript - 快速服务器 POST 方法和获取问题,登录时未定义值

node.js - 切换到输入nodejs的路径

node.js - 如何在 nodejs 中从 goDaddy 安装 SSL 证书?

node.js - express ,多条路线到同一功能

node.js - OpenAI GPT-3 API 错误 : "TypeError: Converting circular structure to JSON" using ExpressJS

iis - 将 IIS 的根目录重定向到应用程序根目录