首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法获取/resources/card

无法获取/resources/card
EN

Stack Overflow用户
提问于 2019-11-12 18:55:04
回答 1查看 70关注 0票数 1

我在尝试用expressJS服务静态文件时遇到了问题。我想使用虚拟路由从文件夹中提供文件。

我的应用程序架构如下所示

代码语言:javascript
复制
├── app
|   ├── assets
|       └── data
|           └── img
|               └── cards
|                   └── * (un bunch of image files)
|   └── index.js
|   └── server.js
├── node_modules
├── package.json
├── package-lock.json

我的index.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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/card/image.png访问我的图像,但是仍然得到这个Cannot GET /resources/cards

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-12 18:58:06

尝试将mount path指定为绝对,并将__dirname添加到path.join(...)

代码语言:javascript
复制
app.use('/resources/cards', express.static(path.join(__dirname, 'assets/data/img/cards')));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58817191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档