express搭建的服务器,如何直接在浏览器里面通过url 访问服务器下某个文件夹下的图片这样的静态资源
发布于 17 小时前 作者 HirayClay 159 次浏览 来自 问答

var constants = require(’./constants/event’); var utils = require(‘util’); var path = require(‘path’) var fs = require(‘fs’) var querystring = require(‘querystring’); var formidable = require(‘formidable’) var bodyParser = require(‘body-parser’); var express = require(‘express’)(); var app = require(‘http’).createServer(express); var io = require(’socket.io‘).listen(app); var url = require(‘url’); var User = require(’./model/user’); var _ = require(‘underscore’); var mongoose = require(‘mongoose’); var port = 3030;

mongoose.connect(“mongodb://localhost/imchat”);

express.use(bodyParser.json()) express.use(bodyParser.urlencoded({extended:true})) express.use(require(‘express’).static(path.join(__dirname,“upload”))) tre.png 这个是我的项目结构,图片都在upload文件夹里面

2 回复

express .use(’/upload’,express.static(‘upload’))

这样不行?

同默认的public文件配置一样,再多配置个就是了 app.use(express.static(‘upload’))

回到顶部