gpt4 book ai didi

javascript - 无法使用nodejs获取?

转载 作者:行者123 更新时间:2023-12-03 02:34:18 26 4
gpt4 key购买 nike

我有一个简单的 Nodejs 应用程序和一些用户。我想使用 URL localhost/users/(username) 加载特定的用户配置文件,但当我尝试加载 URL 时收到此错误:

Cannot GET /users/test

这是我的用户页面的 user.js 路由文件:

var express = require('express');
var router = express.Router();
var User = require('../models/user');

// GET user by username
router.get('/users/:username', function(req, res) {
var username = req.params.username;
res.send(req.params);
res.render('user');
});

module.exports = router;

我的 View 文件夹中有一个 user.handlebars 文件,因此它应该加载该文件。我的路线文件中缺少什么?任何帮助将不胜感激。谢谢!!

编辑:app.js:

console.log('Starting app.js');

const fs = require('fs');
const _ = require('lodash');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const mongo = require('mongodb');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/tipcup');
var db = mongoose.connection;

const routes = require('./routes/index');
const users = require('./routes/users');
const user = require('./routes/user');

// Init App
var app = express();

// View Engine
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'layout'}));
app.set('view engine', 'handlebars');

// BodyParser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use(cookieParser());

// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));

// Express Session
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));

// Passport Init
app.use(passport.initialize());
app.use(passport.session());

// Express Validator
app.use(expressValidator ({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;

while(namespace.length){
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
};
}
}));

// Connect Flash
app.use(flash());

// Global Vars
app.use(function (req, res, next){
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
next();
});

app.use('/', routes);
app.use('/users', users);
app.use('/users/:username', user);

// Set Port
app.set('port', (process.env.PORT || 3000));

app.listen(app.get('port'), function(){
console.log('Server started on port '+app.get('port'));
});

最佳答案

渲染函数渲染 View 并将渲染的 HTML 字符串发送到客户端。看一下 documentation 。但是您尝试归档的是先发送数据,然后渲染并再次发送。因此,只需删除带有 send 的行即可。

router.get('/users/:username', function(req, res) {
var username = req.params.username;
res.render('user');
});

并且,请通过添加带有中间件的代码来编辑您的问题并添加您的错误。

更新

看看您如何为用户定义路线。您必须从 users.js 文件中定义路由中删除 /users

关于javascript - 无法使用nodejs获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48598516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com