无法获取 /resources/cards
Cannot GET /resources/cards
我在尝试使用 expressJS 提供静态文件时遇到问题。
我想使用虚拟路由从文件夹提供文件。
我的应用架构是这样的
├── app
| ├── assets
| └── data
| └── img
| └── cards
| └── * (un bunch of image files)
| └── index.js
| └── server.js
├── node_modules
├── package.json
├── package-lock.json
我的index.js
const express = require('express');
const path = require('path');
const app = express();
//Serving images
app.use('resources/cards', express.static(path.join('assets/data/img/cards')));
app.get('/', function (req, res) {
res.send('Hello World');
});
module.exports = app;
我的server.js
const app = require('./index');
const config = require('./config');
//Mysql connection
const mysql = require('mysql');
app.listen(config.express.port, function() {
console.log(`Running on ${config.express.ip}:${config.express.port}`);
});
const connection = mysql.createConnection({
host: config.mysql.host,
user: config.mysql.username,
password: config.mysql.password,
port: config.mysql.port,
database: config.mysql.database,
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
if (err) throw err;
console.log(`The solution is ${rows[0].solution}`);
});
//connection.end();
我想通过 localhost:3000/resources/cards/image.png 访问我的图像,例如,但仍然得到这个 Cannot GET /resources/cards
尝试将 mount path 指定为绝对值并将 __dirname
添加到 path.join(...)
:
app.use('/resources/cards', express.static(path.join(__dirname, 'assets/data/img/cards')));
我在尝试使用 expressJS 提供静态文件时遇到问题。 我想使用虚拟路由从文件夹提供文件。
我的应用架构是这样的
├── app
| ├── assets
| └── data
| └── img
| └── cards
| └── * (un bunch of image files)
| └── index.js
| └── server.js
├── node_modules
├── package.json
├── package-lock.json
我的index.js
const express = require('express');
const path = require('path');
const app = express();
//Serving images
app.use('resources/cards', express.static(path.join('assets/data/img/cards')));
app.get('/', function (req, res) {
res.send('Hello World');
});
module.exports = app;
我的server.js
const app = require('./index');
const config = require('./config');
//Mysql connection
const mysql = require('mysql');
app.listen(config.express.port, function() {
console.log(`Running on ${config.express.ip}:${config.express.port}`);
});
const connection = mysql.createConnection({
host: config.mysql.host,
user: config.mysql.username,
password: config.mysql.password,
port: config.mysql.port,
database: config.mysql.database,
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
if (err) throw err;
console.log(`The solution is ${rows[0].solution}`);
});
//connection.end();
我想通过 localhost:3000/resources/cards/image.png 访问我的图像,例如,但仍然得到这个 Cannot GET /resources/cards
尝试将 mount path 指定为绝对值并将 __dirname
添加到 path.join(...)
:
app.use('/resources/cards', express.static(path.join(__dirname, 'assets/data/img/cards')));