我需要一个帮助。我无法使用Node.js获取索引页面中包含的外部文件链接。我在下面解释我的代码。
server.js:
var port=8888;
var express=require('express');
var http=require('http');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var expressSession = require('express-session');
var cookieParser = require('cookie-parser'); // the session is stored in a cookie, so we use this to parse it
var app = express();
var server=http.createServer(app);
var io=require('socket.io')(server);
//var admin=require('./route/route.js');
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(morgan('dev')); // log every request to the console
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json
app.use(methodOverride()); // simulate DELETE and PUT
app.use(cookieParser());
app.use(expressSession({secret:'odiyaDoctor'}));
app.get('/admin',function(req,res){
res.sendfile('admin/view/index.html');
});
server.listen(port);
console.log('Magic happens on port'+port); // shoutout to the user
让我解释下面的文件夹结构。
root-
->admin
->js
->angular.min.js
->css
->style.css
->responsive.css
->view
->index.html
->public
->server.js
->package.json
从上面的结构说在根文件夹里面我有一个名为admin
的文件夹。在admin文件夹中,有css,js,view etc
这样的文件夹,用于包含相应的文件。
管理员/视图/ index.html中:
<!DOCTYPE html>
<head ng-app="doctorAdmin">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to Odia Doctor Admin Panel</title>
<link href='http://fonts.googleapis.com/css?family=Roboto:400,300,500,700,900' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css' />
<link rel="shortcut icon" href="img/favicon.png" type="image/x-icon">
<!-- Styles -->
<link rel="stylesheet" href="font-awesome-4.2.0/css/font-awesome.css" type="text/css" /><!-- Font Awesome -->
<link rel="stylesheet" href="css/bootstrap.css" type="text/css" /><!-- Bootstrap -->
<link rel="stylesheet" href="css/style.css" type="text/css" /><!-- Style -->
<link rel="stylesheet" href="css/responsive.css" type="text/css" /><!-- Responsive -->
<script src="js/angular.min.js" type="text/javascript"></script>
<script src="adminController/loginController.js" type="text/javascript"></script>
</head>
<body style="background-image: url('images/resource/login-bg.jpg')">
<div class="login-sec" ng-controller="loginController">
<div class="login-sec">
<div class="login">
<div class="login-form">
<span><img src="images/logo.png" alt="" /></span>
<h5><strong>Identify</strong> Yourself</h5>
<form action="#" method="post">
<fieldset><input type="text" placeholder="Username" class="logininputdiv" /><i class="fa fa-user"></i></fieldset>
<fieldset><input type="password" placeholder="Password" class="logininputdiv" /><i class="fa fa-unlock-alt"></i></fieldset>
<label><input type="checkbox" />Remember me</label><button type="button" class="blue" onClick="document.location='dashboard.html'">LOG IN</button>
</form>
</div>
<span>Copyright © 2015 Odia Doctor</span>
</div>
</div><!-- Log in Sec -->
</body>
</html>
在上面的文件中,我已经包含了一些链接,我的问题是我无法获取那些。请帮我解决这个问题。
答案 0 :(得分:0)
在公共文件夹中添加所有链接,因为您在server.js中设置了以下代码
app.use(express.static(__dirname + '/public'));
这意味着所有外部文件都有相对路径,在公共文件夹sp中将你的css文件夹放在公共文件夹中,如果你不想在公共文件夹中将其放在公共文件夹中而不是设置在代码下面
app.use(express.static(__dirname));