[英]NodeJS - Serve Resources Error
I installed express using npm in VS. 我在VS中使用npm安装了express。 But i have empty exception in lines started with app.use in server.js :
但是我在server.js中以app.use开头的行中有空异常:
'use strict';
var http = require('http');
var port = process.env.PORT || 1337;
var express = require('express');
var app = express();
app.use("/css", express.static(path.join(__dirname, 'public', 'css')));
app.use("/fonts", express.static(path.join(__dirname, 'public', 'ttf')));
app.use("/images", express.static(path.join(__dirname, 'public', 'png')));
app.use("/scripts", express.static(path.join(__dirname, 'public', 'js')));
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(loadCore());
}).listen(port);
I want to serve css, ttf, png, jpg and also just js files existed in scripts folder. 我想提供css,ttf,png,jpg,也可以提供scripts文件夹中存在的js文件。 How should i do this?
我应该怎么做?
I finally changed my server like this; 我终于像这样更改了服务器; i write it here in case might help anyone:
我写在这里,以防万一:
'use strict';
var http = require('http');
var path = require('path');
var port = process.env.PORT || 1337;
var express = require('express');
var app = express();
app.use("/css", express.static(path.join(__dirname, 'public', 'css')));
app.use("/fonts", express.static(path.join(__dirname, 'public', 'fonts')));
app.use("/images", express.static(path.join(__dirname, 'public', 'images')));
app.use("/scripts", express.static(path.join(__dirname, 'public', 'scripts')));
app.get('/',//to serve application
function (req, res) {
res.send(loadCore());
}
);
app.get('/service',//to serve service
function (req, res) {
res.send("[SERVICE]");
}
);
app.listen(port);
My css files is in public/css folder; 我的css文件在public / css文件夹中; i mapped it to localhost:port/css/ using this:
我使用以下方式将其映射到localhost:port / css / :
app.use("/css", express.static(path.join(__dirname, 'public', 'css')));
You seem to miss 你好像很想念
var path = require('path');
somewhere at the beginning. 一开始的某个地方。 Otherwise the
path
variable doesn't point to anything. 否则,
path
变量不会指向任何内容。
Similar issue will, however, occur with loadCore
function that doesn't seem to be defined either. 但是,似乎也未定义的
loadCore
函数也会发生类似的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.