我已经设置了一个新的express(nodejs)项目,并设置了IIS环境来托管此应用程序
当我尝试运行url时,它向我显示404未找到错误。
URL:http://localhost/sampleapi/api
http://localhost/sampleapi/users
NotFoundError:找不到 在D:\ AngularJs \ S004 \ app.js:34:8 在Layer.handle上[作为handle_request](D:\ AngularJs \ S004 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在trim_prefix(D:\ AngularJs \ S004 \ node_modules \ express \ lib \ router \ index.js:317:13) 在D:\ AngularJs \ S004 \ node_modules \ express \ lib \ router \ index.js:284:7 在Function.process_params(D:\ AngularJs \ S004 \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(D:\ AngularJs \ S004 \ node_modules \ express \ lib \ router \ index.js:275:10) 在SendStream.error(D:\ AngularJs \ S004 \ node_modules \ serve-static \ index.js:121:7) 在emitOne上(events.js:116:13) 在SendStream.emit(events.js:211:7) 在SendStream.error(D:\ AngularJs \ S004 \ node_modules \ send \ index.js:270:17)
代码文件app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
var deployPath = process.env.deployPath || "";
console.log('=======' + deployPath);
app.all("sampleapi/api", function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end("<h2>REST API Handler found12.</h2>");
});
app.use('sampleapi/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
代码文件www.js
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('./app');
var debug = require('debug')('s004:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
web.config文件
<configuration>
<system.webServer>
<!-- indicates that the hello.js file is a node.js application
to be handled by the iisnode module -->
<handlers>
<add name="iisnode" path="www.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="myapp">
<match url="/*" />
<action type="Rewrite" url="www.js" />
</rule>
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^www.js\/debug[\/]?" />
</rule>
</rules>
</rewrite>
<directoryBrowse enabled="true" />
<iisnode devErrorsEnabled="true" debuggingEnabled="true" loggingEnabled="false" nodeProcessCommandLine="C:\Program Files\nodejs\node.exe" />
</system.webServer>
<appSettings>
<add key="deployPath" value="/sampleapi" />
</appSettings>
</configuration>
文件夹结构