2024年1月8日 星期一

[研究]用Visual Studio 2022 Installer 和 nvm-windows 1.1.12 安裝 node.js v20.10.0 LTS、npm、nvm

[研究]用Visual Studio 2022 Installer 和 nvm-windows 1.1.12 安裝 node.js v20.10.0 LTS、npm、nvm

2024-01-08

因為Visual Studio 2022 Installer 預設安裝的 node.js 是舊版,測試再加上  nvm-windows 1.1.12 可否搭配安裝使用新版。
























package.json

{
  "name": "nodejs-web-app2",
  "version": "0.0.0",
  "description": "NodejsWebApp2",
  "main": "server.js",
  "author": {
    "name": ""
  },
  "devDependencies": {
    "eslint": "^8.21.0"
  },
  "eslintConfig": {
  }
}


server.js

'use strict';
var http = require('http');
var port = process.env.PORT || 1337;

http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello World\n');
}).listen(port);



(下圖)按下 F5 執行

********************************************************************************




package.json

{
  "name": "express-app1",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node app"
  },
  "description": "ExpressApp1",
  "author": {
    "name": ""
  },
  "dependencies": {
    "body-parser": "^1.15.0",
    "cookie-parser": "^1.4.0",
    "debug": "^2.2.0",
    "express": "^4.14.0",
    "morgan": "^1.7.0",
    "pug": "^2.0.0-beta6",
    "serve-favicon": "^2.3.0"
  },
  "devDependencies": {
    "eslint": "^8.21.0"
  },
  "eslintConfig": {
  }
}

app.js

'use strict';
var debug = require('debug')('my express app');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

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

var server = app.listen(app.get('port'), function () {
    debug('Express server listening on port ' + server.address().port);
});


(完)

沒有留言:

張貼留言