简体   繁体   中英

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

the same message also for Cannot GET /images

my index.js file

/*  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...');
  });

how to make images folder accessible and to go to the bike.png by url

Firstly you should remove the trailing slash in app.use(express.static('images/')); as @ptothep already mentioned.

If you don't indicate a route for static assets they will be accessible from a root URL like this localhost:3000/bike.png . If you wish to map images folder to /images then indicate this route like this:

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

See Static files

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.

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