Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require to import for mern developers #179

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions 1-node-farm/final/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const fs = require('fs');
const http = require('http');
const url = require('url');
const slugify = require('slugify');
const replaceTemplate = require('./modules/replaceTemplate');
// const fs = require( 'fs' );
// const http = require('http');
// const url = require('url');
// const slugify = require('slugify');
// const replaceTemplate = require('./modules/replaceTemplate');

import fs from 'fs';
import http from 'http';
import url from 'url';
import slugify from 'slugify';
import replaceTemplate from './modules/replaceTemplate';

/////////////////////////////////
// FILES
Expand Down Expand Up @@ -49,7 +55,7 @@ const tempProduct = fs.readFileSync(
const data = fs.readFileSync(`${__dirname}/dev-data/data.json`, 'utf-8');
const dataObj = JSON.parse(data);

const slugs = dataObj.map(el => slugify(el.productName, { lower: true }));
const slugs = dataObj.map((el) => slugify(el.productName, { lower: true }));
console.log(slugs);

const server = http.createServer((req, res) => {
Expand All @@ -58,17 +64,19 @@ const server = http.createServer((req, res) => {
// Overview page
if (pathname === '/' || pathname === '/overview') {
res.writeHead(200, {
'Content-type': 'text/html'
'Content-type': 'text/html',
});

const cardsHtml = dataObj.map(el => replaceTemplate(tempCard, el)).join('');
const cardsHtml = dataObj
.map((el) => replaceTemplate(tempCard, el))
.join('');
const output = tempOverview.replace('{%PRODUCT_CARDS%}', cardsHtml);
res.end(output);

// Product page
} else if (pathname === '/product') {
res.writeHead(200, {
'Content-type': 'text/html'
'Content-type': 'text/html',
});
const product = dataObj[query.id];
const output = replaceTemplate(tempProduct, product);
Expand All @@ -77,15 +85,15 @@ const server = http.createServer((req, res) => {
// API
} else if (pathname === '/api') {
res.writeHead(200, {
'Content-type': 'application/json'
'Content-type': 'application/json',
});
res.end(data);

// Not found
} else {
res.writeHead(404, {
'Content-type': 'text/html',
'my-own-header': 'hello-world'
'my-own-header': 'hello-world',
});
res.end('<h1>Page not found!</h1>');
}
Expand Down
1 change: 1 addition & 0 deletions 1-node-farm/final/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "node-farm",
"version": "1.0.0",
"type": "module",
"description": "Learning node.js",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 7 additions & 5 deletions 3-asynchronous-JS/final/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const fs = require('fs');
const superagent = require('superagent');
import fs from 'fs';
import superagent from 'superagent';
// const fs = require( 'fs' );
// const superagent = require('superagent');

const readFilePro = file => {
const readFilePro = (file) => {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) reject('I could not find that file 😢');
Expand All @@ -12,7 +14,7 @@ const readFilePro = file => {

const writeFilePro = (file, data) => {
return new Promise((resolve, reject) => {
fs.writeFile(file, data, err => {
fs.writeFile(file, data, (err) => {
if (err) reject('Could not write file 😢');
resolve('success');
});
Expand All @@ -34,7 +36,7 @@ const getDogPic = async () => {
`https://dog.ceo/api/breed/${data}/images/random`
);
const all = await Promise.all([res1Pro, res2Pro, res3Pro]);
const imgs = all.map(el => el.body.message);
const imgs = all.map((el) => el.body.message);
console.log(imgs);

await writeFilePro('dog-img.txt', imgs.join('\n'));
Expand Down
1 change: 1 addition & 0 deletions 3-asynchronous-JS/final/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "3-asynchronous-js",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
13 changes: 9 additions & 4 deletions 4-natours/after-section-06/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const express = require('express');
const morgan = require('morgan');
import express from "express";
import morgan from "morgan";
import tourRouter from "./routes/tourRoutes";
import userRouter from "./routes/userRoutes";

const tourRouter = require('./routes/tourRoutes');
const userRouter = require('./routes/userRoutes');
// const express = require( 'express' );
// const morgan = require('morgan');

// const tourRouter = require('./routes/tourRoutes');
// const userRouter = require('./routes/userRoutes');

const app = express();

Expand Down
3 changes: 2 additions & 1 deletion 4-natours/after-section-06/controllers/tourController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
import fs from "fs";
// const fs = require( 'fs' );

const tours = JSON.parse(
fs.readFileSync(`${__dirname}/../dev-data/data/tours-simple.json`)
Expand Down
1 change: 1 addition & 0 deletions 4-natours/after-section-06/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "natours",
"version": "1.0.0",
"type": "module",
"description": "Learning node, express and mongoDB",
"main": "app.js",
"scripts": {
Expand Down
7 changes: 5 additions & 2 deletions 4-natours/after-section-06/routes/tourRoutes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const express = require('express');
const tourController = require('./../controllers/tourController');
import express from "express";
import tourController from "./../controllers/tourController"

// const express = require('express');
// const tourController = require('./../controllers/tourController');

const router = express.Router();

Expand Down
7 changes: 5 additions & 2 deletions 4-natours/after-section-06/routes/userRoutes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const express = require('express');
const userController = require('./../controllers/userController');
import express from "express";
import userController from "./../controllers/userController"

// const express = require( 'express' );
// const userController = require('./../controllers/userController');

const router = express.Router();

Expand Down
8 changes: 6 additions & 2 deletions 4-natours/after-section-06/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const dotenv = require('dotenv');
const app = require('./app');
import dotenv from "dotenv";
import app from "./app";

// const dotenv = require( 'dotenv' );
// const app = require('./app');


dotenv.config({ path: './config.env' });

Expand Down