I installed express using npm in VS. But i have empty exception in lines started with app.use in server.js :
'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. 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; i mapped it to localhost:port/css/ using this:
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.
Similar issue will, however, occur with loadCore
function that doesn't seem to be defined either.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.