Cov txheej txheem:

Node.js Webpage Ntu 2: 7 Cov Kauj Ruam
Node.js Webpage Ntu 2: 7 Cov Kauj Ruam

Video: Node.js Webpage Ntu 2: 7 Cov Kauj Ruam

Video: Node.js Webpage Ntu 2: 7 Cov Kauj Ruam
Video: Node.js Ultimate Beginner’s Guide in 7 Easy Steps 2024, Kaum ib hlis
Anonim
Node.js Webpage Ntu 2
Node.js Webpage Ntu 2

Txais tos rau PART 2 !!

Nov yog ntu 2 rau kuv Node.js lub vev xaib thov kev qhia. Kuv tsoo qhov kev qhia no ua ob ntu raws li nws cais cov uas tsuas xav tau kev qhia luv luv thiab cov uas xav tau kev qhia tag nrho ntawm lub vev xaib.

Kuv yuav mus hla kuv qhov chaw tsim. Koj li yuav txawv, yog li ua raws li kuv thiab kawm cov txuj ci siv. Thaum koj xaiv tus qauv HTML sib txawv ntws yuav txawv me ntsis xwb. Nco ntsoov qhov no.

Kauj Ruam 1: Tus Qauv App

App Qauv
App Qauv

Yog li kuv lub xaib ua raws lub tshuab hluav taws xob nthuav tawm, txawm li cas los xij kuv siv cov tes tuav ntau dua li cov pob zeb ci. Yog tias koj nyiam jade mus rau nws! Jade yog tes luv HTML tsis muaj tag nrho cov kab zauv thiab div's. Yog tias koj tsis nkag siab tias koj yuav xav mus ntsib youtube thiab saib qee qhov kev qhia HTML.

Kuv nyiam thiab xis nyob dua nrog HTML thiab tuav tes yog li qhov kuv tau siv. Txhawm rau tsim qhov phiaj xwm nthuav qhia nrog cov tuav tes khiav qhov hais kom ua sai.

qhia --hbs nameofmyapp

Tom qab ntawd txuas ntxiv ua raws cov kauj ruam hauv Ntu 1 rau kev txhim kho txhua qhov khoom nruab nrab.

Express tsim cov qauv app tshwj xeeb heev thiab muaj txiaj ntsig zoo ib qho node.js daim ntawv thov ua raws daim ntawv no nrog qee qhov kev hloov pauv.

Hauv daim duab txuas nrog koj tuaj yeem pom cov ntawv sib txawv thiab cov ntawv, hauv qab no kuv sim piav qhia txhua yam ntawm no.

rau

Nov yog daim nplaub tshev uas tau khiav ua ntej thaum node.js pib koj lub server. Nws zoo li www cov ntaub ntawv thiab ua raws cov ntawv no rau kev ua tiav. Cov ntaub ntawv www qhia node.js kom pib server ntawm chaw nres nkoj 3000 (qhov no tuaj yeem hloov pauv mus rau ib yam dab tsi) thiab ua qee yam ntxiv xws li mloog cov xwm txheej thiab lwm yam. Qhov tseem ceeb tshaj plaws yog qhov chaw nres nkoj uas koj app tau teeb tsa.

node_modules

Hauv daim nplaub tshev no yog dab tsi hu ua khoom nruab nrab. Cov khoom nruab nrab Kuv nyiam piav qhia raws li software ntxiv los ua kom yooj yim rau koj los ua tus lej. Lawv yog ib txwm muaj lwm lub tsev qiv ntawv nrog ua haujlwm ua ntej rau koj siv. Qee qhov khoom nruab nrab ntxiv uas kuv siv rau txoj haujlwm no yog Nodemailer, Passport, Nodemon, bycrypt, thiab lwm yam.

pej xeem

Nov yog qhov twg tag nrho koj cov duab, CSS, thiab javascript rau koj lub vev xaib yuav mus. Cov no tau siv ncaj qha los ntawm lub vev xaib.

txoj kev

Cov no yog qhia txoj hauv kev rau koj lub xaib. Xws li lub vev xaib, nplooj ntawv nkag mus, thiab lwm yam.

saib

Raws li koj tuaj yeem pom cov kev pom yog.hbs cov ntaub ntawv lossis.handlebars, ib qho twg yuav ua haujlwm nws tsuas yog siv qee qhov kev siv ntawm app.js cov ntaub ntawv. Cov no yog koj cov nplooj ntawv html nplooj ntawv uas yuav tshwm rau ntawm qhov browser. Cov txheej txheem yog koj cov ntaub ntawv txheej txheem tseem ceeb thiab qee zaum nyob hauv nws tus kheej txheej txheej ntu. Cov ntaub ntawv txheej txheem tseem ceeb hu rau koj lwm cov ntaub ntawv tuav thiab qhia lawv, qhov no yuav ua rau muaj kev nkag siab ntau ntxiv thaum peb dhia mus rau hauv txoj cai.

app.js ua

Nov yog koj cov ntaub ntawv app tseem ceeb, qee zaum qhov no hu ua server, tsuas yog nyob ntawm teeb tsa. Cov ntaub ntawv no muaj txhua qhov kev teeb tsa rau tus neeg rau zaub mov thiab txawm tias qee qhov haujlwm tshwj xeeb. Nws tseem yuav yog tus ua yuam kev.

pob.json

Cov ntaub ntawv no tau tsim los ntawm kev tshaj tawm thiab qhia npm txhua qhov nruab nrab uas koj xav siv hauv koj txoj haujlwm. Thaum koj ua npm nruab, tag nrho cov khoom nruab nrab hu ua tawm hauv cov ntawv no, yuav raug teeb tsa hauv node_modules nplaub tshev.

Kauj ruam 2: Layout Koj Tus Qauv

Koj tuaj yeem tsim txhua yam ntawm koj li HTML los ntawm kos lossis koj tuaj yeem siv tus qauv. Kuv tau siv tus qauv rau lub xaib no. Lwm qhov chaw uas kuv tau pab tsim kuv tau sau los ntawm kos. Qhov kev xaiv yog koj li, qib no piav qhia tus qauv txheej txheem.

Kuv lub vev xaib thov siv tus qauv bootstrap uas zoo heev ntawm kev ua CSS zoo. Txhawm rau nrhiav cov qauv mus xyuas lub xaib no. Zoo li tau hais ua ntej hauv cov kauj ruam dhau los txhua qhov xav tau css, js, thiab img cov ntaub ntawv nyob hauv qab cov ntawv pej xeem. Cov ntaub ntawv no ua rau lub xaib zoo dua li cov ntawv yooj yim thiab nws siv cov duab li cas hauv lub xaib.

Txhawm rau ua kom cov tes tuav cov qauv ua haujlwm nrog tus qauv Cov nplooj ntawv tau muab faib ua ob daim. Thawj yog qhov uas tau hais los ua "txheej txheem". Cov txheej txheem yog cov khoom uas koj xav kom tshwm ntawm txhua nplooj ntawv web hauv koj lub xaib. Hauv kuv qhov xwm txheej no yog lub taub hau, uas muaj lub cim qhia chaw, thiab cov ntawv hauv qab, uas yog tuav cov lus qhia ntxiv thiab cov khoom tso tawm.

Cov ntaub ntawv txheej txheem thiab lwm cov ntaub ntawv tuav yog nyob hauv cov ntawv pom. Kuv yuav hla qhov txheej txheem yooj yim dua los ntawm lub tshuab hluav taws xob nthuav qhia koj tau siv ua ntej los qhia seb lub tswv yim ua haujlwm li cas, tom qab ntawd koj tuaj yeem pom kuv tus lej thiab sib piv lawv.

Qhia tawm qhov tsim tawm.handlebars cov ntaub ntawv

{{title}} {{{lub cev}}}

Qhov tseeb khawv koob tuav yog nyob hauv tus tuav tes {{title}} thiab {{{lub cev}}}. Yog li ob qho no ua qhov sib txawv {{title}} yog qhov sib txawv uas tau dhau los ntawm index.js cov ntaub ntawv hauv txoj kev, ib zaug dhau mus rau tus qauv nws tau tshwm sim. Tag {{{lub cev}}} siv qhov uas tau hu ua hauv kev ua haujlwm hauv koj txoj kev js cov ntaub ntawv. Hauv peb qhov xwm txheej index.js muaj kab ntawv no:

res.render ('index', {title: 'Express', suav: userCount});

Qhov no hu rau 'ntsuas' cov ntaub ntawv ntawm qhov uas koj tau siv lub cav, jade, tus tuav tes, thiab lwm yam, yog li hauv peb cov ntaub ntawv index.handlebars.

Qhia tawm index.handlebars

{{npe}}

Zoo siab txais tos rau {{title}}

Cov ntawv index.handlebars tau dhau mus zoo li qhov sib txawv rau {{{lub cev}}} lossis thiab tso tawm ntawm koj lub vev xaib.

Qhov no tso cai rau koj kom muaj qhov zoo li qub ntawm koj lub vev xaib thiab ib feem sib txawv. Qhov no ua rau headers thiab footers zoo li koj tsis tas yuav rov ua dua nplooj ntawv tag nrho, thaum thauj khoom nplooj ntawv tshiab, tsuas yog qee cov ntaub ntawv raug hloov pauv.

Kauj Ruam 3: Hu rau Daim Ntawv

Hu rau daim ntawv
Hu rau daim ntawv
Hu rau daim ntawv
Hu rau daim ntawv
Hu rau daim ntawv
Hu rau daim ntawv

Kuv suav nrog daim foos tiv toj rau hauv kuv lub vev xaib yog li leej twg tuaj yeem xa email rau kuv tus xaib email, nrog cov lus nug lossis lus pom.

Daim ntawv tiv toj no siv npm nruab nrab-ntim khoom uas hu ua Node Mailer.

Teeb tsa Node Mailer

Txhawm rau nruab node-mailer koj tsuas yog yuav tsum tau ua cov cai hauv qab no hauv koj cov ntaub ntawv qib siab tshaj plaws, hauv peb qhov xwm txheej, myapp.

sudo npm nruab nodemailer

Thaum teeb tsa koj yuav tsum teeb tsa ob peb yam hauv koj daim ntawv thov app.js.

Thawj zaug tsuas yog kev vam khom, qhov no qhia ntawm peb tias peb npaj yuav siv cov khoom nruab nrab no.

var nodemailer = xav tau ('nodemailer');

Qhov thib ob yog peb tus neeg xa khoom, tus xa khoom siv los txuas rau koj li server, hauv kuv li gmail.

// Tus neeg xa khoom siv tau txais gmail account

var transporter = nodemailer.createTransport ({kev pabcuam: 'gmail', auth: {hom: 'OAuth2', neeg siv: '[email protected]', clientId: '139955258255-a3c6ilqu6rtocigde7cbrusicg7j00eh.apps.googleusercontent.com': 'Q775xefdHA_BGu3ZnY9-6sP-', refreshToken: '1 / 0HfdzyzW3FmnDPqeYkv19_py6zWgMCOqI9DSZ9kQWfc', accessToken: 'ya29. GlvDBGA2Z_coEKjQOnXAnBLbTB0wQmS-sARqNGC3V2UATiywNb34IhFq4d7UQvhTobE6pi83-FB2-OvMWjC-mk-EKPMYmwxFe9AOZ7mY6kurYyQ7e1Mu8m8INxg7'}})

yog tias siv nodemailer nrog tus xa ntawv sib txawv thov saib ntawm no rau cov ntaub ntawv thiab kev pab.

Ob peb yam yuav hloov pauv ntawm ib tus neeg mus rau lwm tus: tus siv, tus thov kev pabcuam, tus thov kev pabcuam. refreshToken, thiab accessToken.

Koj tus userId yog email uas koj xav siv, Kuv tau ua qhov tshiab muaj npe zoo ib yam li kuv lub xaib.

ClientId, clientSecret, refreshToken, thiab accessToken xav tau los ntawm koj tus lej Google.

Yog koj xav tau kev pab ntau ntxiv koj tuaj yeem ua raws daim vis dis aus no.

Thaum tag nrho cov haujlwm no tau ua tiav peb yuav ntxiv peb cov lus qhia ntxaws.

Tom ntej no peb yuav tsum tau lees paub tias txhua qhov haujlwm hauv peb daim ntawv tau nkag mus thiab yog cov lus teb raug.

// Express Validatorapp.use (expressValidator ({errorFormatter: function (param, msg, value) {var namespace = param.split ('.'), Hauv paus = namespace.shift (), formParam = hauv paus; thaum (namespace.length) {formParam + = '[' + namespace.shift () + ']';} xa rov qab {param: formParam, msg: msg, nqi: tus nqi};}})));

Tam sim no peb yuav tsum tau txais cov ntaub ntawv los ntawm peb daim ntawv tiv toj ntawm peb lub vev xaib thiab xa xov.

// Tshaj tawm los ntawm kev sib cuag xa khawm, xav tau los tsim lub vev xaib nrog cov lus ua tiav rau xa daim ntawv thov app.post ('/contact_Form', ua haujlwm (req, res) {// Tau txais cov ntaub ntawv tawm ntawm daim ntawv sib cuag, los ntawm homepage.hbs var npe = req.body.name; var email = req.body.email; var phone = req.body.phone; var message = req.body.message; var mailOptions = {// tsim cov ntaub ntawv siv thaum xa xov los ntawm: ' Tsis Siv Neeg Email ', mus rau:' [email protected] ', ntsiab lus:' Lub Vev Xaib Hu Xov Tooj: ' + lub npe, ntawv nyeem:' Koj tau txais cov lus tshiab los ntawm koj lub vev xaib hu rau daim ntawv. / N / n ' +' Ntawm no yog cov ntsiab lus: / n / nName: ' + name +' / n / nEmail: ' + email +' / n / nPhone: ' + phone +' / n / nMessage: / n ' + message} transporter.sendMail (mailOptions, function (err, res) {if (err) {console.log ('Error');} else {console.log ('Email Sent');}}) res.render ('index'); // muab lub vev xaib tshiab, saib yuav ua li cas ua qhov no nrog cov lus ua tiav, zoo li tawm ntawm nplooj ntawv})

Nyem

Flash tau siv los qhia cov lus tom qab ua tiav. Koj tuaj yeem pom qhov no thaum koj xa daim ntawv, lossis tsis nkag mus rau qhov chaw kom raug.

Nruab flash ib yam li lwm yam npm nruab nrab.

sudo npm nruab txuas-flash

var flash = xav tau ('txuas-flash'); // muaj qhov ua haujlwm flash los qhia ntawm cov lus tshuaj ntsuam

// Txuas Flashapp.use (nyem ());

Pab kom nyem uas thawb thiab hloov kho cov lus ntawm lub vev xaib. Nov yog cov lus uas hais tej yam xws li kev ua tiav, lossis cov ntaub ntawv tau nkag mus tsis raug.

// Ntiaj teb 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 ('yuam kev'); res.locals.user = req.user || tsis muaj; tom ntej ();});

Qee qhov xav tau qhov hloov pauv cuam tshuam nrog flash.

Muaj koj mus rau daim ntawv hu rau.

Kauj ruam 4: ID nkag mus nplooj ntawv

ID nkag mus nplooj ntawv
ID nkag mus nplooj ntawv

Nov tsuas yog qee yam kuv xav pom yog tias kuv tuaj yeem ua tau thiab tej zaum kuv yuav siv nws yav tom ntej. Kuv tsuas yog xav piav qhia txoj cai raws li nws nyob hauv kuv qhov chaw cia khoom git.

Yog li feem no siv ob peb ntau npm nruab nrab-ntim khoom. Nruab qhov hauv qab no siv cov lus txib hauv qab no.

npm nruab daim ntawv hla tebchaws && npm nruab daim ntawv hla tebchaws-hauv zos && npm nruab bcryptjs

&& tso cai rau koj los khiav ntau cov lus txib nrog ib kab.

ID nkag mus thiab cov neeg siv

Koj yuav tsum tau tsim tus ID nkag mus.js thiab cov ntaub ntawv user.js hauv qab koj cov ntawv tais ceev tseg. Qhov no yuav raug siv los tso cai tsim cov neeg siv, uas yuav muab khaws cia rau hauv peb cov ntaub ntawv khaws cia, thiab tso cai rau tus neeg siv nkag mus los ntawm kev txheeb xyuas lub hauv paus.

user.js

var nthuav tawm = xav tau ('nthuav tawm'); var router = express. Router (); var phau ntawv hla tebchaws = xav tau ('phau ntawv hla tebchaws'); var LocalStrategy = xav tau ('daim ntawv hla tebchaws-zos'). Lub tswv yim; var Tus neeg siv = xav tau ('../ qauv/neeg siv'); // Sau npe router.get ('/register', muaj nuj nqi (req, res) {res.render ('register');}); // Sau npe Tus neeg siv router.post ('/register', muaj nuj nqi (req, res) {var name = req.body.name; var email = req.body.email; var username = req.body.username; var password = req.body.password; var password2 = req.body.password2; // Validation req.checkBody ('name', 'Name is required'). notEmpty (); req.checkBody ('email', 'Email is required').notEmpty (); req.checkBody ('email', 'Email tsis siv tau'). isEmail (); req.checkBody ('username', 'Username is required'). notEmpty (); req.checkBody (' lo lus zais ',' Yuav tsum tau lo lus zais '). notEmpty (); req.checkBody (' password2 ',' Passwords tsis sib xws '). sib npaug (req.body.password); var errors = req.validationErrors (); yog (yuam kev) {res.render ('sau npe', {yuam kev: yuam kev});} ntxiv {var newUser = Tus Neeg Siv Tshiab ({lub npe: npe, email: email, username: username, password: password}); User.createUser (newUser, ua haujlwm (yuam kev, tus neeg siv) {yog (yuam kev) pov yuam kev; console.log (neeg siv);}); req.flash ('ua tiav_msg', 'Koj tau sau npe thiab tam sim no tuaj yeem nkag mus'); res.redirect (' /nkag mus ');}});

Rhuav tshem qhov no ib thooj

Ua ntej peb suav tag nrho cov khoom nruab nrab xav tau, tom qab ntawd peb suav nrog peb cov qauv ntaub ntawv uas tau piav qhia hauv qab no. Peb khiav los ntawm daim ntawv sau npe thiab tso saib peb cov ntawv sau npe tuav. Tom qab ntawd txoj haujlwm tseem ceeb los. Cov no tso cai rau peb rau npe tus neeg siv tshiab hauv peb lub database. Lub luag haujlwm txheeb xyuas kom paub tseeb tias txhua daim teb raug siv thiab suav nrog hauv daim ntawv, yog tias tsis yog nws yuav qhia sai rau lawv. Tom ntej no nws kuaj xyuas qhov ua yuam kev, thiab yog tias tsis muaj qhov yuam kev tshwm sim nws tsim tus neeg siv tshiab nrog cov ntaub ntawv muab. Tom qab ntawv nws rov mus rau nplooj ntawv nkag mus, tso cai rau koj nkag mus.

login.js

var nthuav tawm = xav tau ('nthuav tawm');

var router = express. Router (); var phau ntawv hla tebchaws = xav tau ('phau ntawv hla tebchaws'); var LocalStrategy = xav tau ('daim ntawv hla tebchaws-zos'). Lub tswv yim; var Tus neeg siv = xav tau ('../ qauv/neeg siv'); /* Tau txais cov neeg siv npe. */// Homepage router.get ('/', muaj nuj nqi (req, res) {res.render ('ID nkag mus');}); passport.use (LocalStrategy tshiab (ua haujlwm (username, password, ua tiav) {User.getUserByUsername (username, function (err, user) {if (err) throw err; if (! user) {return done (null, false, { lus: 'Tsis Paub Tus Neeg Siv'});} User.comparePassword (password, user.password, muaj nuj nqi (yuam kev, isMatch) {yog (yuam kev) pov yuam kev; yog (isMatch) {rov qab ua tiav (tsis siv neeg,)) rov qab ua tiav (tsis muaj tseeb, tsis tseeb, {lus: 'Tus lej tsis raug'});}});});}))); passport.serializeUser (muaj nuj nqi (neeg siv, ua tiav) {ua tiav (tsis muaj, user.id);}); passport.deserializeUser (muaj nuj nqi (id, ua tiav) {User.getUserById (id, muaj nuj nqi (yuam kev, neeg siv) {ua tiav (yuam kev, neeg siv);});}); router.post ('/login', passport.authenticate ('local', {successRedirect: '/', failRedirect: '/login', failFlash: true}), muaj nuj nqi (req, res) {res.redirect ('/ dashboard ');}); router.get ('/logout', muaj nuj nqi (req, res) {req.logout (); req.flash ('success_msg', 'Koj tau tawm hauv'); res.redirect ('/homepage');};

module.exports = router;

Ua ntej peb suav tag nrho cov khoom nruab nrab xav tau, tom qab ntawd peb suav nrog peb cov qauv ntaub ntawv uas tau piav qhia hauv qab no. Peb txoj hauv kev los ntawm tus lej nkag mus thiab tso saib peb cov ntawv nkag mus rau tus lej nkag. Peb tom qab ntawd siv qee daim ntawv hla tebchaws ua haujlwm coj tus neeg siv lub npe thiab tus lej nkag mus thiab txheeb xyuas lawv tawm tsam peb cov ntaub ntawv khaws tseg. Peb yuav siv tus lej zais zais uas tuaj yeem nkag mus qeeb me ntsis ntawm raspberry pi. Kuv piav qhia qhov no ntxiv tom ntej. Tom qab siv tus neeg siv lub npe thiab tus password, koj raug xa mus rau lub vev xaib uas yuav qhia lub dashboard raws li peb teeb tsa qhov no hauv peb cov ntaub ntawv ntsuas. Peb kuj ntxiv ntawm no lub peev xwm nkag mus.

Zoo li kuv tau hais yav tas los peb tseem yuav xav tsim tus qauv txhawm rau txheeb xyuas lub hauv paus rau.

Qhov no yog ua tiav los ntawm kev tsim cov ntawv tais ceev tseg hauv koj daim ntawv thov tseem ceeb hu ua qauv. Hauv daim nplaub tshev no cov ntaub ntawv user.js kuj tseem xav tau.

qauv/user.js

var mongoose = xav tau ('mongoose');

var bcrypt = xav tau ('bcryptjs'); // User Schema var UserSchema = mongoose. Schema ({username: {type: String, index: true}, password: {type: String}, email: {type: String}, name: {type: String}}); var Tus Neeg Siv = module.exports = mongoose.model ('Tus Neeg Siv', Tus Neeg Siv Khoom);

module.exports.createUser = muaj nuj nqi (newUser, hu rov qab) {

bcrypt.genSalt (10, ua haujlwm (yuam kev, ntsev) {bcrypt.hash (newUser.password, ntsev, ua haujlwm (yuam kev, hash) {newUser.password = hash; newUser.save (rov qab);});}); } module.exports.getUserByUsername = muaj nuj nqi (username, callback) {var lus nug = {username: username}; User.findOne (nug, hu rov qab); } module.exports.getUserById = function (id, callback) {User.findById (id, callback); } module.exports.comparePassword = muaj nuj nqi (tus neeg sib tw tus lej, hash, rov qab) {bcrypt.compare (tus neeg sib twPassword, hash, muaj nuj nqi (yuam kev, isMatch) {yog (yuam kev) pov tseg; callback (tsis muaj, isMatch);}); }

Tus qauv no piav qhia qhov peb cov neeg siv tsis zoo yuav zoo li cas thiab peb yuav nkag mus rau lawv li cas. Kuv tau hais ua ntej tias peb yuav encrypting peb tus password. qhov no yog li tsis muaj leej twg lo lus zais khaws hauv lub database thaum muaj kev ua txhaum. Cov lus zais tau hashed siv cov khoom nruab nrab bcrypt.

Kauj Ruam 5: Cov Tsheb Npav

Tsheb Caij Nyoog
Tsheb Caij Nyoog

Kuv xav pom muaj pes tsawg tus neeg siv tshwj xeeb tau mus ntsib kuv lub vev xaib thiab suav tus lej "hits". Muaj ntau txoj hauv kev los ua qhov no, Kuv yuav piav qhia tias kuv mus txog qhov twg.

Qhov no siv mongodb sau los taug qab pes tsawg tus neeg siv tau mus ntsib kuv nplooj ntawv thiab pes tsawg zaus txhua tus neeg tuaj txawv tebchaws tuaj xyuas.

Txij li thaum peb tau tham txog teeb tsa mongoDB kuv yuav tsis hla nws dua.

Tej zaum koj yuav xav tau ntxiv ob qhov kev sau rau koj lub hauv paus txhawm rau txhawm rau ua ke. Txhawm rau ua qhov no koj tuaj yeem nruab RoboMongo yog tias siv UI, txawm li cas los xij yog tias koj siv lub taub hau raspberry pi zoo li kuv yog koj yuav lom zem cov lus txib hauv qab no.

Mongo plhaub

Txhawm rau hloov kho db, tau txais cov ntaub ntawv, lossis tsim sau koj yuav xav tau lub plhaub mongo ntawm chav tsis muaj taub hau.

Khiav

mongo

Qhov no yuav qhib lub plhaub.

Ntxiv sau

Hauv kuv qhov xwm txheej, cov ntaub ntawv hu ua loginapp, koj tuaj yeem sau nws txhua yam koj xav tau.

siv nameofyourdb

Peb xav tau kev sau los tuav tag nrho peb tus IP chaw nyob ntawm cov neeg siv uas mus ntsib peb lub xaib.

db.creatCollection ("ip")

Tom ntej no peb tsim cov khoom sau kom suav qhov sib txawv tshwj xeeb rau peb lub xaib. Qhov no tau pib nrog tus lej thiab suav pib ntawm 0.

db.createCollection ("suav", {id: "hit counter", suav: 0})

Taug qab IP Chaw Nyob

Txhawm rau ua qhov no peb yuav rub cov neeg siv Ip thaum lawv mus ntsib peb lub vev xaib, nce peb suav, thiab khaws lawv los sib piv lawv tom qab.

Peb yuav tsum tsim qee cov qauv los khaws peb cov mongoose schemas, thiab ntxiv qee qhov chaws rau peb cov homepage.js cov ntaub ntawv.

Peb tsim count.js thiab ip.js thiab khaws lawv hauv peb cov qauv nplaub tshev.

Cov ntaub ntawv ip.js tsuas yog txheej txheem rau peb tus IP chaw nyob

var mongoose = xav tau ('mongoose'); // pob tes tuav rau mongo

// Suav Schema var IpSchema = mongoose. Schema ({ip: {hom: Txoj hlua,}, suav: {hom: Tus lej,}}); var Ip = module.exports = mongoose.model ('Ip', IpSchema);

count.js yuav raug hu los ntawm peb lub vev xaib los pib ua qhov kev ntaus. Qhov no ua tiav raws li hauv qab no.

//Homepagerouter.get('/ ', muaj nuj nqi (req, res) {publicIp.v4 (). Tom qab ntawd (ip => {Public_ip = ip; console.log ("ipv4:"+ Public_ip); // =>' 46.5.21.123 '}); publicIp.v6 (). Ces (ip => {console.log ("ipv6" + ip); Public_ip = ip; // =>' fe80:: 200: f8ff: fe21: 67cf ' });

Count.getCount (sau, ipc, Public_ip, muaj nuj nqi (suav) {

}); suav = db.collection ('suav'). findOne ({id: "hit counter"}, muaj nuj nqi (yuam kev, suav) {userCount = count.count; res.render ('homepage', {suav: userCount}); }); });

Qhov no tshwm sim txhua lub sijhawm ib tus neeg mus rau peb lub vev xaib, hauv qhov no theinternet.onthewifi.com/homepage.

Nws tshuaj xyuas tus IP ntawm tus neeg siv, ip4 lossis ip6, thiab tom qab ntawd khaws cov nqi uas nws xa mus rau suav.get.collection uas yog lub luag haujlwm khaws cia hauv peb cov ntaub ntawv count.js.

Tom qab txheeb xyuas qhov tsis sib xws ntawm tus neeg siv nws tom qab ntawd rov qab thiab tshaj tawm tus lej suav rau hauv lub vev xaib raws li tus tes tuav sib txawv.

Cov ntaub ntawv count.js yog raws li hauv qab no.

//count.jsvar mongo = xav tau ('mongodb'); // txhawb database var mongoose = xav tau ('mongoose'); // pob tuav tswj rau mongo mongoose.connect ('mongodb: // localhost/loginapp'); var db = mongoose.connection; var Ip = xav tau ('../ qauv/ip'); // Suav Schema var CountSchema = mongoose. Schema ({id: {hom: hlua,}, suav: {hom: Tus lej,}}); var suav = module.exports = mongoose.model ('Suav', CountSchema); module.exports.getCount = muaj nuj nqi (suav, ipc, Public_ip, hu rov qab) {// suav yog sim, hu rov qab tsis ua haujlwm ipc.findOne ({ip: Public_ip}, ua haujlwm (yuam kev, iptest) {yog (! iptest) // ntxiv tus ip tshiab yog tsis nyob hauv lub database, thiab hloov kho tshiab {var new_ip = tshiab Ip ({ip: Public_ip, suav: 1}); db.collection ('ip'). tseg (new_ip); // ntxiv ip tshiab rau database count.update (// hloov tshiab ntaus counter {id: "hit counter"}, {$ inc: {count: 1}})} lwm yam // hloov kho tshwj xeeb ip counter, kom pom leej twg mus ntsib ntau tshaj {ipc.update ({ip: Public_ip}, {$ inc: {suav: 1}})}}); }

Qhov no tsim cov phiaj xwm suav, thiab peb.getCount muaj nuj nqi.. GetCount muaj nuj nqi tshawb xyuas DB rau tus neeg siv ip thiab yog tias nws pom nws, kev ua haujlwm nce ntxiv suav ntawm tus neeg siv ntawd, tsis yog lub txee ntaus. Txawm li cas los xij yog tias cov neeg siv ip tsis pom nws yuav tsim cov khoom sib sau tshiab nrog cov neeg siv ip thiab nce tus lej ntaus los ntawm 1.

Qhov no rov qab los thiab nthuav tawm rau lub vev xaib.

Muaj koj muaj nws tus ip taug qab ntaus ntaus.

Kauj Ruam 6: Blog

Blog
Blog

Tam sim no kuv tab tom sim txhim kho blog nruab nrab ntawm kuv qhov kev txaus siab txog software, tsev ntse, thiab Polaroids. Yog li kuv tsim ntu blog. Lub blog siv cov nplooj ntawv html zoo li qub thiab cov tuav tes tuav. Tom qab saib mus rau cov thev naus laus zis zoo dua los ua blogging yooj yim dua kuv tau rov tsim dua kuv lub vev xaib siv hugo. Hugo yog lub tshuab hluav taws xob html zoo li qub. Kuv tham ntau ntxiv txog qhov no hauv cov lus qhia hauv qab no.

Kauj ruam 7: Tiav lawm

Muaj koj mus qhia qhov tob ntawm kuv tus node.js lub vev xaib tuav hauv zos ntawm kuv tus raspberry pi. Yog tias koj muaj lus nug lossis lus pom thov tawm hauv qab no.

Kuv vam tias qhov no yuav pab lwm tus nyob ntawd.

Txog qhov sib txawv mus rau qhov chaw no siv hugo, lub vev xaib zoo li lub tshuab hluav taws xob pom kuv lwm qhov kev qhia (yuav los sai sai no).

Pom zoo: