Tell us what’s happening:
I am getting // running tests
5. Your client should connect to your server.
// tests completed for Set up the Environment
code for client.js
$(document).ready(function () {
/*global io*/
let socket = io();
// Form submittion with new message in field with id 'm'
$('form').submit(function () {
var messageToSend = $('#m').val();
$('#m').val('');
return false; // prevent form submit from refreshing page
});
});
and server
'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const routes = require('./routes.js');
const auth = require('./auth.js');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.set('view engine', 'pug');
app.set('views', './views/pug');
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(passport.initialize());
app.use(passport.session());
fccTesting(app); // For fCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
myDB(async client => {
const myDataBase = await client.db('database').collection('users');
routes(app, myDataBase);
auth(app, myDataBase);
io.on('connection', socket => {
console.log('A user has connected');
});
}).catch(e => {
app.route('/').get((req, res) => {
res.render('index', { title: e, message: 'Unable to connect to database' });
});
});
const PORT = process.env.PORT || 3000;
http.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
I have git id and secret set in the .env
###Your project link(s)
solution: https://3000-freecodecam-boilerplate-xuhf289jwg6.ws-us118.gitpod.io
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Challenge Information:
Advanced Node and Express - Set up the Environment
Post your routes.js
code as well.
It would be easier if you just posted a repo.
1 Like
ILM
April 4, 2025, 8:23am
4
we can’t access your workshop, if you want to share a workshop you need to share a snapshot of it: Collaboration & Sharing of Gitpod workspaces - Gitpod Enterprise
ILM
April 10, 2025, 8:28am
6
I am looking at the code you shared, I do not see a get
route for /chat
, can you share the code for that?
this is the one I have and it towards the bottom
const passport = require('passport');
const bcrypt = require('bcrypt');
module.exports = function (app, myDataBase) {
app.route('/').get((req, res) => {
res.render('index', {
title: 'Connected to Database',
message: 'Please log in',
showLogin: true,
showRegistration: true,
showSocialAuth: true
});
});
app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => {
res.redirect('/profile');
});
app.route('/profile').get(ensureAuthenticated, (req,res) => {
res.render('profile', { username: req.user.username });
});
app.route('/logout').get((req, res) => {
req.logout();
res.redirect('/');
});
app.route('/register').post((req, res, next) => {
const hash = bcrypt.hashSync(req.body.password, 12);
myDataBase.findOne({ username: req.body.username }, (err, user) => {
if (err) {
next(err);
} else if (user) {
res.redirect('/');
} else {
myDataBase.insertOne({
username: req.body.username,
password: hash
},
(err, doc) => {
if (err) {
res.redirect('/');
} else {
// The inserted document is held within
// the ops property of the doc
next(null, doc.ops[0]);
}
}
)
}
})
},
passport.authenticate('local', { failureRedirect: '/' }),
(req, res, next) => {
res.redirect('/profile');
}
);
app.route('/auth/github').get(passport.authenticate('github'));
app.route('/auth/github/callback').get(passport.authenticate('github', { failureRedirect: '/' }), (req, res) => {
req.session.user_id = req.user.id;
res.redirect("/chat");
})
app.use((req, res, next) => {
res.status(404)
.type('text')
.send('Not Found');
});
}
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
};
ILM
April 10, 2025, 6:22pm
8
izabela.stamatova:
res.redirect("/chat");
I see this, this is not a route for the /chat
endpoint, this is a redirect after authentication
client.js
is already inside the public
folder, update that one. Do not create another one in the root folder.
1 Like
That was the problem is working now. Thank you!