新手写express的遇见奇葩问题
发布于 7 年前 作者 ThomasHuke 2879 次浏览 来自 问答

我自己写的express 本地可以运行,无论是 什么端口都可以运行,但是我放到腾讯云上就gg,好尴尬,然后我就想这到底是为什么?我就用官方自己生成的去运行,果然 本地可以 云服务器也是可以运行,新手伤不起我就奇怪了如果我代码是错误的 为什么 本地是可以使用呢???? github上文件地址

6 回复
const express=require('express'); //引入了express模块
const path=require('path');//引入path模块
const favicon=require('serve-favicon');//引入 sever-favicon
const app=express();//express实例化
const router=express.Router();
const routers=require(__dirname+'/routes/routers.js');//引入外部模块 router模块
const multer=require('multer');
app.set('views',__dirname+'/views');
app.set('view engine','jade');//设置模块引擎
app.use(express.static(path.join(__dirname,'/public')));//设置中间件的文件夹是 public文件夹(其中_dir)//used 是express注册中间件的一个方法叫做used
app.use(favicon(__dirname+'/public/favicon.ico'));//设置favicon文件的真实位置,used 引入express的一个中间件 其中get post方法也是used的变种;
routers(router,app);
app.listen(80,'localhost',()=>{
    console.log(`浏览器成功启动\nhttp://localhost:80`);
}).on('error',(err)=>{
    console.log(err.message);
});
Contact GitHub API Training Shop Blog About

module.exports=(router,app)=> { router.get(’/’, (req, res)=> { res.render(‘index’,{title:“solo school”},(err, html)=> { res.send(html); }) }); router.get(’/callMe’, (req, res)=> { res.render(‘callme’,(err, html)=> { res.send(html) }) }); router.get(’/root1973’, (req, res)=> { res.render(‘myRoot’,(err, html)=> { res.send(html); }) }); router.get(’/upload’,(req,res)=> { res.render(‘myRoot’,(err, html)=> { res.send(html); }) }); app.use(’/’,router); console.log(路由模块已经成功的启动); }

上面分别是 app.js文件和router文件

报什么错啊

打印日志看看,腾讯云应该可以看日志的。在上面换个端口试一下

app.listen(80,'localhost',()=>{
    console.log(`浏览器成功启动\nhttp://localhost:80`);
})

请把你的localhost去掉,监听在127.0.0.1,也就是说只接受来自本地回环的连接

回到顶部