繁体   English   中英

如何使 node.js mysql 中的图像文件夹可访问

[英]how to make a folder of images accessible in node.js mysql

i wrote an api node.js mysql and i created a folder called images in the same directory with index.js file, but when go to http://localhost:3000/images/bike.png a message says that Cannot GET /images/bike .png

同样的消息也适用于无法 GET /images

我的 index.js 文件

/*  RESTFUL SERVICES BY NODEJS */

var express = require('express');
var mysql = require('mysql');
var bodyParser = require('body-parser');


//connect to mysql
var con = mysql.createConnection({
    host: 'localhost' ,
    user:  'root' ,
    password: '',
    database: 'nodeapp'
})


var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static('images/'));

//connect to database
con.connect((err) =>{
    if(err) throw err;
    console.log('Mysql Connected...');
  });

/Server listening
app.listen(3000,() =>{
    console.log('Server started on port 3000...');
  });

如何使图像文件夹可访问以及 go 到 bike.png by url

首先,您应该删除app.use(express.static('images/'));中的斜杠。 正如@ptothep 已经提到的。

如果您没有为 static 资产指明路线,则可以从根 URL 访问它们,例如localhost:3000/bike.png 如果您希望 map images文件夹到/images然后像这样指示这条路线:

app.use('/images', express.static('images'));

参见Static 文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM